I am very new to Selenium and TestNG. I am going to validate a login page by passing both valid and invalid data to user name field and password field. Test data passed through an excel sheet. I want to mention each username field and password field in different @Test .I am using following code .When I used different @Test it does not get excel sheet data because it mention in @BeforeTest.Although I used dependsOnMethods it does not get the values. Can any one help me there.
public class Login {
WebDriver driver;
WebDriverWait wait;
XSSFWorkbook workbook;
XSSFSheet sheet;
XSSFCell cell;
@BeforeTest
public void browserSetup() throws IOException {
System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
FileInputStream file = new FileInputStream(new File("D:\\codtest.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i ) {
driver = new ChromeDriver();
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.manage().window().maximize();
String userName = sheet.getRow(i).getCell(0).getStringCellValue();
String passWord = sheet.getRow(i).getCell(1).getStringCellValue();
}
}
@Test(dependsOnMethods = {"browserSetup"})
public void userName(){
driver.findElement(By.id("txtUsername")).sendKeys(userName);
WebElement erro_message=driver.findElement(By.xpath("//*[@id=\"spanMessage\"]")).getText()
//error message when username filed is empty
Boolean Display = erro_message.isDisplayed();
if (Display == true) {
System.out.println("User Name field is required");
}
}
@Test
public void passWord(){
driver.findElement(By.id("txtPassword")).sendKeys(userName);
WebElement erro_message=driver.findElement(By.xpath("//*[@id=\"spanMessage\"]")).getText()
//error message when password filed is empty
Boolean Display = erro_message.isDisplayed();
if (Display == true) {
System.out.println("Password field is required");
}
}
}
CodePudding user response:
You should annotate @BeforeMethod
.
Sorry mate, would you try to replace the annotation that @BeforeTest with @BeforeMethod? and remove the dependsOnMethods from the searchProduct method?
Be like,
@BeforeMethod
public void browserSetup() throws IOException {
System.out.println("browserSetup");
}
@Test
public void searchProduct(){
System.out.println("searchProduct");
}
@Test
public void location(){
System.out.println("location");
}
Then your test will execute as follows.
[RemoteTestNG] detected TestNG version 6.14.3
browserSetup
location
browserSetup
searchProduct
PASSED: location
PASSED: searchProduct
If you want to execute your tests in a specific order, you can add priority into the method.
@BeforeMethod
public void browserSetup() throws IOException {
System.out.println("browserSetup");
}
@Test(dependsOnMethods = {"browserSetup"}, priority = 2)
public void searchProduct(){
System.out.println("searchProduct");
}
@Test(priority=1)
public void location(){
System.out.println("location");
}
CodePudding user response:
Please use DataProvider option in your test method as the example below,
Parametrization can be done by following ways,
- Using data provider
- Using excel input in the test method(all inputs/one test)
- Convert your excel input to TestNG data provider in before test method
Example:
@DataProvider(name = "loginCredentials")
public Object[][] loginDetails(){
return new Object[][] {{"[email protected]", "Password@123"}, {"[email protected]", "Password@123"}, {"[email protected]", "Password@123"}};
}
@Test(groups = {"dp"}, dataProvider="loginCredentials")
public void justLogin(String username, String password){
System.out.println("Username : " username " / Password : " password);
}