I'm learning Selenium Webdriver in Java together with TestNG. I'm testing with Google Login page.
I'm having trouble with running test cases sequentially. What I want to do is:
But what I experienced is:
I spent 2 days but couldn't figure how. My code below:
=================================================================
package TestNG;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;
public class GoogleAccountLogin {
WebDriver driver;
@BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver","F:\\path\\geckodriver.exe");
driver=new FirefoxDriver();
}
@Test(priority=0)
public void LoginSuccessful() throws InterruptedException {
// Go to google account
driver.manage().window().maximize();
driver.get("https://accounts.google.com");
Thread.sleep(3000);
// Check if the page is correct
String currentTitle = driver.getTitle();
Assert.assertEquals(currentTitle, "Sign in - Google Accounts");
// Enter email and submit
WebElement email = driver.findElement(By.id("Email"));
email.clear();
email.sendKeys("validemail");
WebElement Next = driver.findElement(By.id("next"));
Next.click();
Thread.sleep(1000);
// Enter password
WebElement password = driver.findElement(By.id("Passwd"));
password.clear();
password.sendKeys("validpassword");
WebElement Login = driver.findElement(By.id("signIn"));
Login.click();
Thread.sleep(5000);
// Check if login successful
currentTitle = driver.getTitle();
Assert.assertEquals(currentTitle, "My Account");
}
@Test(priority=1)
public void LoginFailInvalidEmail() throws InterruptedException {
// Go to google account
driver.manage().window().maximize();
driver.get("https://accounts.google.com");
Thread.sleep(3000);
// Check if the page is correct
String currentTitle = driver.getTitle();
Assert.assertEquals(currentTitle, "Sign in - Google Accounts");
// Enter email and submit
WebElement email = driver.findElement(By.id("Email"));
email.clear();
email.sendKeys("falseemail");
WebElement Next = driver.findElement(By.id("next"));
Next.click();
Thread.sleep(1000);
// Check error message and login state
String errorMess = driver.findElement(By.id("errormsg_0_Email")).getText();
Assert.assertEquals(errorMess, "Sorry, Google doesn't recognize that email.");
currentTitle = driver.getTitle();
Assert.assertEquals(currentTitle, "Sign in - Google Accounts");
}
@AfterTest
public void tearDown() {
driver.close();
driver.quit();
}
}
From the TestNG documentation :
@BeforeTest : The annotated method will be run before any test method belonging to the classes inside the tag is run.
@AfterTest : The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
If you want to run something before and/or after each test-method you need to use @BeforeMethod
and @AfterMethod
:
@BeforeMethod : The annotated method will be run before each test method.
@AfterMethod : The annotated method will be run after each test method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.