问题描述
我的目标是让一系列测试一个接一个地运行.我希望有一个登录"脚本将用户登录,然后以下脚本在同一窗口/驱动程序中继续运行.我正在使用TestNG,因此如果有帮助,可以在testng.xml文件中设置我的测试套件.
My goal is to have a series of tests run one after the other. I would like to have a "login" script log the user in and then the following scripts kick off continuing in the same window/driver. I'm using TestNG so my test suite is setup in the testng.xml file if that helps.
public class LoginScript {
String username, password, siteid;
private WebDriver driver;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
static Logger log = Logger.getLogger(LoginScript.class);
@BeforeSuite (alwaysRun=true)
@Parameters({ "url","username","password","site" })
public void setUp(String env, String user, String pwd, String ste) throws Exception {
username=user;
password=pwd;
siteid=ste;
driver = new FirefoxDriver();
driver.get(env);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testLoginScript() throws Exception {
//Maximize window
driver.manage().window().maximize();
//Login
driver.findElement(By.id("TBSiteID")).clear();
driver.findElement(By.id("TBSiteID")).sendKeys(siteid);
driver.findElement(By.id("TBUserName")).clear();
driver.findElement(By.id("TBUserName")).sendKeys(username);
driver.findElement(By.name("TBPassword")).clear();
driver.findElement(By.name("TBPassword")).sendKeys(password);
driver.findElement(By.name("Login")).click();
Thread.sleep(2000);
log.info("Found requested site");
}
@AfterSuite
public void tearDown() throws Exception {
//driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
我要运行的下一个脚本:
Next script that I would like to run:
public class AddNormalEE {
String username, password, siteid;
private WebDriver driver;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
static Logger log = Logger.getLogger(AddNormalEE.class);
@BeforeSuite (alwaysRun=true)
@Parameters({ "url","username","password","site" })
public void setUp(String env, String user, String pwd, String ste) throws Exception {
username=user;
password=pwd;
siteid=ste;
//driver = new FirefoxDriver();
//driver.get(env);
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testAddNormalEE() throws Exception {
//Maximize window
//driver.manage().window().maximize();
@AfterSuite
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
推荐答案
如果测试是依赖的,则可以使用在method2上定义的dependsOnMethod = {method1}将它们放在同一类中,从而保证顺序.如果在不同的类之间,则可以考虑从LoginScript类扩展您的AddNormalEE类.
If your tests are dependent, you can put them in the same class with dependsOnMethod={method1} defined on method2 so that the order is gauranteed. If it is between different classes, you might consider extending your AddNormalEE class from the LoginScript class.
要在同一浏览器中运行测试,您的驱动程序实例需要在您的类之间共享,或者在所有@Tests中必须相同.使其为静态,或考虑使用threadlocal webdriver变量,以防您计划有一天并行运行.在上述情况下,您还可以在loginScript中使用方法getDriver(),以便在需要避免静态操作的情况下将驱动程序对象返回给AddNormalEE类.
To run tests in the same browser, your driver instance needs to be shared between your classes or it has to be the same in all your @Tests. Either make it static or consider having a threadlocal webdriver variable in case you plan to run parallely some day.In the above case, you can also have a method getDriver() in your loginScript which returns the driver object to the AddNormalEE class if static needs to be avoided.
通常,进行独立测试是件好事.您可以利用并行运行来克服独立测试的时间问题.将登录作为一种方法而不是一种测试,因为我们没有根据您上面的代码声明任何行为.如果我正在测试登录,则将有单独的测试仅测试登录功能.
As a general practice, it is good to have independent tests. You can make use of parallel runs to overcome the time issue with independent tests. Making login as a method and not a test since we are not asserting any behavior as per your code above. If I am testing login, I would have separate tests that test login functionality only.
这篇关于Selenium Webdriver:如何在同一窗口中一个接一个地运行多个测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!