从另一个包调用webdriver

时间:2016-07-26 16:46:31

标签: java selenium

我正在尝试使用Selenium和Java建立一个Web应用程序测试套件。我将在src

下创建3个包
  • 对象 - 用于我的页面对象
  • 任务 - 用于测试方法
  • 测试 - 用于测试

在Tasks下,我创建了一个名为CommonTasks的类,用于存储为测试而创建的方法。这里有些例子。

protected void verifyNumberOfElements(By selector, int expectedsize){
        int size = driver.findElements(selector).size();
        log.info("INFO: Verifying the number of elements is "+expectedsize+"");
        Assert.assertEquals(size, expectedsize);
        log.info("PASS: The number of elements returned was "+expectedsize+" ");
    }

public static void verifyText(By selector, String expectedtext){
        //verify that the expected text is present
        String actualtext = driver.findElement(selector).getText();
        Assert.assertEquals(actualtext, expectedtext);
        log.info("PASS: "+expectedtext+" was present and verified");
    }

protected void verifyElement(By selector){
        //Verify that a certain selector is present in the page
        smartSleep(selector);
        boolean isPresent = driver.findElements(selector).size() > 0;
        Assert.assertEquals(isPresent, true);
        log.info("PASS: Element was found");
        boolean notPresent = driver.findElements(selector).size() > 0;
        Assert.assertEquals(notPresent, false);
        log.info("FAIL: Element was NOT found");
    }

在Tests包下,我创建了一个名为ABC的类来测试功能ABC。我有一些基本步骤,如下面

verifyText(PageObjects.ItemText, "Multiple Choice - Single Answer Radio - Vertical");
verifyText(PageObjects.Progress_PercentComplete, "0%");

我遇到的问题是我不知道在哪里创建webdriver。我希望能够创建许多测试类并调用在任务包中创建的任何方法。我知道我需要从Tasks导入类,但无法弄清楚webdriver创建部分。任务和测试包都将引用驱动程序,那么如何使其工作?是否需要在Tasks.CommonTasks或Tests.ABC中创建?

我还需要测试连接到SauceLabs而不是本地机器。

1 个答案:

答案 0 :(得分:0)

从上面的代码中,Tasks包中的所有方法都是实用程序方法,并且对于测试套件是通用的,所以这些方法只在已经初始化驱动程序的Test方法中调用,因此在测试类中创建webdriver并且将其传递给任务包中的实用程序方法。

希望有所帮助