java 如何仅使用一个 Web 驱动程序实例在 testng 套件中运行多个测试类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34283798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run multiple test classes in testng suite with only one web driver instance?
提问by Lokesh Sanapalli
I have testng.xml file as below...
我有 testng.xml 文件如下...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
<test name="Test">
<classes>
<class name="com.cigital.myssi.automation.tests.SoftwareTest"/>
<class name="com.cigital.myssi.automation.tests.LoginTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
I have the corresponding test classes as below....
我有相应的测试类如下....
SoftwareTest.java
软件测试.java
public class SoftwareTest extends Verification {
private WebDriver driver;
private SoftwarePage softwarepage;
private LoginPage loginpage;
private Log log = LogFactory.getLog(SoftwareTest.class);
@BeforeClass(alwaysRun = true)
public void setup() throws InterruptedException {
driver = DriverFactory.getDriverInstance();
softwarepage = new SoftwarePage(driver);
loginpage = new LoginPage(driver);
}
@Test(description="Create Critical Software")
public void CreateCriticalProject()
{
log.info("Create Critical Software test case Started...............");
assertEquals(true, loginpage.gotoSignPage());
assertEquals(true,loginpage.LogintoPortal("[email protected]", "Pass@123", "Welcome"));
assertEquals(true,softwarepage.CreateCriticalSoftware(DataGenerator.RandomNumber("SAMPLE")));
assertEquals(true,loginpage.Logout());
}
@AfterClass(alwaysRun = true)
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
LoginTest.java
登录测试.java
public class LoginTest extends Verification {
private WebDriver driver;
private LoginPage loginPage;
@BeforeClass(alwaysRun = true)
public void setup() throws InterruptedException {
driver = DriverFactory.getDriverInstance();
loginPage = new LoginPage(driver);
}
@Test(description = "SigninPage")
public void SigninPage() {
assertEquals(true, loginPage.gotoSignPage());
}
@Test(description="Login to Portal",dependsOnMethods="SigninPage",dataProviderClass=CredentialsProvider.class,dataProvider="testCasesLogin")
public void LoginToPortal(UserBean newbean) throws InterruptedException
{
assertEquals(true,loginPage.LogintoPortal(newbean.getUsername(),newbean.getPassword(),newbean.getTitle()));
}
@AfterClass(alwaysRun = true)
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
I have initiated webdrive in both the classes. The problem is whenever the first test i.e. SoftwareTest is executed then the browser quits. But, I want to execute the next test i.e. LoginTest in order. Can anyone help me how can I achieve that?
我在这两个课程中都启动了 webdrive。问题是每当执行第一个测试,即 SoftwareTest,浏览器就会退出。但是,我想按顺序执行下一个测试,即 LoginTest。任何人都可以帮助我如何实现这一目标?
Regards,
问候,
回答by murali selenium
Try below new class
试试下面的新课程
public class Config extends Verification{
public static WebDriver driver;
public SoftwarePage softwarepage;
public LoginPage loginpage;
@BeforeSuite
public void setUp(){
driver = DriverFactory.getDriverInstance();
}
@AfterSuite
public void tearDown(){
if (driver != null) {
driver.quit();
}
}
}
}
extends this class to all other test classes some thing like below
将此类扩展到所有其他测试类,如下所示
public class SoftwareTest extends Config {
private Log log = LogFactory.getLog(SoftwareTest.class);
@BeforeClass(alwaysRun = true)
public void setup() throws InterruptedException {
softwarepage = new SoftwarePage(driver);
}
here no need of AfterClass i hope and also in BeforeClass delete this 'driver = DriverFactory.getDriverInstance();'
我希望这里不需要 AfterClass 并且在 BeforeClass 中删除这个 'driver = DriverFactory.getDriverInstance();'
Let me know if you have any issues..
如果您有任何问题,请告诉我。
Thanks
谢谢
回答by Paras
You are creating a driver instance for both the classes in BeforeClassmethod, so once the class is executed AfterClassmethod is called and it is closing your driver.
您正在为BeforeClass方法中的两个类创建一个驱动程序实例,因此一旦执行该类,AfterClass就会调用方法并关闭您的驱动程序。
Instead of using BeforeClass& AfterClassmethods you can use BeforeSuitemethod to launch the browser and AfterSuitemethod to quit the driver. This would solve your purpose.
您可以使用method 启动浏览器和退出驱动程序的方法,而不是使用BeforeClass&AfterClass方法。这将解决您的目的。BeforeSuiteAfterSuite
This BeforeSuiteand AfterSuitemethod you can create in any of the 2 classes that you have created.
您可以在已创建的 2 个类中的任何一个中创建此BeforeSuite和AfterSuite方法。

