Skip to content

Instantly share code, notes, and snippets.

@PramodDutta
Created August 16, 2023 03:23
Show Gist options
  • Select an option

  • Save PramodDutta/5f88f68c32aa5e9b05d554a9ce99da0c to your computer and use it in GitHub Desktop.

Select an option

Save PramodDutta/5f88f68c32aa5e9b05d554a9ce99da0c to your computer and use it in GitHub Desktop.
Find the Terminated Employee in Orange HR and delete it
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class WebTablesTest {
private WebDriver driver;
@BeforeTest
public void setUp() {
driver = new FirefoxDriver();
driver.get("https://awesomeqa.com/hr/web/index.php/auth/login");
driver.manage().window().maximize();
}
@Test
public void testWebTables() throws InterruptedException {
driver.findElement(By.xpath("//input[@placeholder='Username']")).sendKeys("");
driver.findElement(By.xpath("//input[@placeholder='Password']")).sendKeys("");
driver.findElement(By.xpath("//button[normalize-space()='Login']")).click();
Thread.sleep(5000);
System.out.println("Employee - " + findEmployeeStatus("Aman"));
}
@AfterTest
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
private String findEmployeeStatus(String employeeName) {
int row = findRowsCount();
int col = findColsCount();
String firstPart = "//div[@role='table']/div[2]/div[";
String secondPart = "]/div/div[";
String thirdPart = "]";
String employeeStatus = null;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
String dynamicPath = firstPart + i + secondPart + j + thirdPart;
WebElement dataElement = driver.findElement(By.xpath(dynamicPath));
String data = dataElement.getText();
if (data.contains(employeeName)) {
String employeeStatusPath = dynamicPath + "/following-sibling::div[3]";
WebElement statusElement = driver.findElement(By.xpath(employeeStatusPath));
employeeStatus = statusElement.getText();
System.out.println(employeeName + " employee status is " + employeeStatus);
if (employeeStatus.equals("Terminated")) {
String editStatusPath = dynamicPath + "//following-sibling::div/div/button[1]";
driver.findElement(By.xpath(editStatusPath)).click();
}
break;
}
}
}
return employeeStatus;
}
private int findRowsCount() {
return driver.findElements(By.xpath("//div[@role='table']/div[2]/div")).size();
}
private int findColsCount() {
return driver.findElements(By.xpath("//div[@role='table']/div[2]/div[1]/div/div")).size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment