我有两个必须执行的测试用例
1.登录应用
2.执行一些操作
以下是我的代码设计:
BaseTest.java
public abstract class BaseTest {
public WebDriver driver;
@BeforeSuite
public void openApplication() {
System.setProperty(chrome_key,chrome_value);
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get(url);
}
}
LoginPage.java
public class LoginPage extends BasePage{
@FindBy (xpath = "//input[@id='username']")
WebElement userName;
@FindBy (xpath = "//input[@id='password']")
WebElement password;
public LoginPage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
public void loginToApp(String username, String pwd) {
}
}
Account.java:
public class NewAccount extends BasePage{
@FindBy(xpath = "//span[text()='Accounts']/../..")
WebElement accountsTab;
public NewAccount(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
public void createNewAccount() {
}
}
LoginToApp.java:
public class LoginToApp extends BaseTest {
@Test
public void a_verifyLogin() throws IOException {
LoginPage hp = new LoginPage(driver);
hp.loginToApp(username, password);
}
}
CreateAccount.java:
public class CreateAccount extends BaseTest {
@Test
public void b_createAccountRecord() {
NewAccount na = new NewAccount(driver);
na.createNewAccount();
}
}
testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="com.testcases.LoginToApp"/>
<class name="com.testcases.CreateAccount"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
有了这个框架结构,当我执行 testng.xml 文件时,LoginToApp 中的第一个测试将按预期执行。当控件进入 CreateAccount 中的测试时,驱动程序变为 Null,因此无法执行第二个测试。
预期流量:
1.初始化浏览器
2.启动网址
3.执行LoginToApp.java的@Test方法
4.执行CreateAccount.java的@Test方法
是否可以在不将 WebDriver 设置为静态的情况下实现上述流程?如果是,请解释一下。
最佳答案
1.Initialise Browser (set this under before method)
2.Launch the url (give priority =0)
3.Execute the @Test method of LoginToApp.java (give priority =1)
4.Execute the @Test method of CreateAccount.java( give depends on method )
depends on method only execute if your loginapp test successfully run
关于java - 通过 testng 执行一个又一个的 Selenium 测试,无需重新启动浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62413044/