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;
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…