2016-07-26 26 views
0

我正在尝试使用Selenium和Java设置一个Web应用程序测试套件。我会在src创建3个包从另一个包中调用webdriver

  • 对象 - 用于我的页面对象
  • 任务 - 用于测试的方法
  • 测试 - 用于测试

在我创建的任务一个名为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"); 
    } 

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

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

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

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

回答

0

从上面的代码可以看出,Tasks包中的所有方法都是实用方法,并且对于测试套件来说很常见,因此只有在驱动程序已初始化的情况下,才能从Test方法调用这些方法,以便在测试类中创建您的webdriver并将其传递给任务包中的实用程序方法。

希望它有帮助