[英]Selenium and TestNG all test in one browser
I have abstract class, where initialize webdriver. 我有抽象类,初始化webdriver。 All classes with the implementation of the tests are inherited from him.
所有执行测试的类都是从他继承的。 I want to reduce the time of test, open a browser for all tests.
我想减少测试时间,为所有测试打开浏览器。
class AbstractClassCase {
public static WebDriver driver;
@BeforeClass
@Parameters({"webDriver", "applicationHost", "applicationPort", "driverHost", "driverPort", "username", "password"})
public void setUp(
String webDriverIdentifier,
String applicationHost,
@Optional String applicationPort,
@Optional String driverHost,
@Optional String driverPort,
@Optional String username,
@Optional String password){
driver = new FirefoxDriver();
driver.get("localhost");
login(username, password)
}
@AfterСlass
public void tearDown() {
driver.quite();
}
}
public class TestButton extends AbstractClassCase {
@Test
public void testClickButtonNo() {
WebElement button = driver.findElement(By.id("button-no"));
button.click();
WebElement status = driver.findElement(By.id("button-status"));
Assert.assertEqual("Cancel", status.getText());
}
}
and other test class in the same spirit. 和其他测试课一样的精神。
How can I reconfigure this class, so that the browser opened once? 如何重新配置此类,以便浏览器打开一次?
If you want to Open Browser at the start once and run all the methods and then close it. 如果要在开始时打开浏览器并运行所有方法,然后关闭它。
There are 2 steps i would recommend to follow. 我建议遵循两个步骤。
Step 1 : Include browser invocation code in methods with @BeforeSuite && @AfterSuite for closing the browser/driversession. 步骤1 :在@BeforeSuite && @AfterSuite的方法中包含浏览器调用代码,以关闭浏览器/ driversession。 This makes sure these tests are run once before and after test suite.
这确保了这些测试在测试套件之前和之后运行一次。
protected static WebDriver Browser_Session;
@BeforeSuite
public void beforeSuite() {
Browser_Session=new FirefoxDriver();
Browser_Session.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
}
@AfterSuite
public void afterSuite() {
Browser_Session.quit();
}
Step2 : Open testng.xml and include all such classes (test methods) under a single Suite, Thus making sure the browser (via Selenium) is invoked first and then rest all methods are run in the same browser. 步骤2 :打开testng.xml并在单个Suite下包含所有这些类(测试方法),从而确保首先调用浏览器(通过Selenium),然后在同一浏览器中运行所有方法。
Here " Class Init " contains Browser Initiating code and ClassA & ClassB are subclassess of Init. 这里“ 类Init ”包含浏览器启动代码,ClassA和ClassB包含Init的子类。
Hope this helps 希望这可以帮助
If you want to run all test in one instance, i wrote my own WebDriverFactory so here is some examples for help: 如果你想在一个实例中运行所有测试,我编写了自己的WebDriverFactory,所以这里有一些帮助示例:
public static WebDriver getDriver(){
if (driver == null) {
return new FireFoxDriver();
} else {
return driver;
}
}
Now remove AfterClass and add this to your class it will shutdown your browser in the end 现在删除AfterClass并将其添加到您的类中它最终将关闭您的浏览器
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
dismissDriver();
} catch (Exception e) {
}
}
});
}
public static void dismissDriver() {
if (driver != null) {
try {
driver.quit();
driver = null;
} catch (Throwable t) {
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.