I'm trying to test the login functionality of a website.
I want my first test case to provide invalid credentials with the first set of data that I defined in the DataProvider method. Than I want to execute the first assert [Assert.assertTrue(errorMessage.isDisplayed());]
, and verify that I get an error message.
In the second test case I want to provide valid credentials with the second set of data that I defined in the DataProvider method. Than I want to execute the second assert [Assert.assertTrue(userSettings.isDisplayed());]
, and verify that the user settings icon is displayed.
Since the test method runs twice and both assert statements are running one test always passes and one test always fails.
How do I use assert in this scenario?
Here's the code:
package testClasses;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class LetsKodeItLogin {
WebDriver driver;
@DataProvider(name="login")
public Object[][] getData() {
return new Object[][] {
{"Oren@email.com", "test"},
{"test@email.com", "abcabc"}
};
}
@BeforeClass
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.get("https://letskodeit.teachable.com");
}
@Test(dataProvider="login")
public void letsKodeIt(String usernameEmail, String password) throws InterruptedException {
WebElement loginLink = driver.findElement(By.xpath("//a[contains(@href,'/sign_in')]"));
loginLink.click();
WebElement emailField = driver.findElement(By.id("user_email"));
emailField.sendKeys(usernameEmail);
WebElement passwordField = driver.findElement(By.id("user_password"));
passwordField.sendKeys(password);
WebElement loginButton = driver.findElement(By.name("commit"));
loginButton.click();
WebElement errorMessage = driver.findElement(By.xpath("//div[contains(text(),'Invalid email or password')]"));
WebElement userSettings = driver.findElement(By.xpath("//div[@id='navbar']//img[@alt='test@email.com']"));
// Assert.assertTrue(errorMessage.isDisplayed());
// Assert.assertTrue(userSettings.isDisplayed());
}
@AfterMethod
public void afterEachMethod() throws InterruptedException {
Thread.sleep(1000);
}
@AfterClass
public void tearDown() {
driver.quit();
}
}