Skip to content

Instantly share code, notes, and snippets.

@pragmatictesters
Created December 26, 2018 19:41
Show Gist options
  • Save pragmatictesters/47c19f825b791d7d6880fb10e0148ed7 to your computer and use it in GitHub Desktop.
Save pragmatictesters/47c19f825b791d7d6880fb10e0148ed7 to your computer and use it in GitHub Desktop.
Selenium WebDriver Example : Add a new employee
package com.pragmatic.selenium.hrm;
import com.github.javafaker.Faker;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
/**
* Created by hansi on 2018-12-26
**/
public class AddEmployeeTest {
private WebDriver driver;
private Faker faker;
private static final String BASE_URL = "http://hrm.pragmatictestlabs.com";
@BeforeClass
public void beforeClass() {
WebDriverManager.chromedriver().setup();
}
@BeforeMethod
public void beforeMethod() {
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.navigate().to(BASE_URL);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("txtUsername")).sendKeys("Admin");
driver.findElement(By.id("txtPassword")).sendKeys("Ptl@#321");
driver.findElement(By.id("btnLogin")).click();
faker = new Faker();
}
@AfterMethod
public void afterMethod() {
driver.close();
}
@Test
public void testAddEmployees() {
String firstName = "User" + faker.name().firstName();
String profilePic = "/Users/hansi/Desktop/playground/Ex_Files_Java_SE_8_NF/Exercise Files/01_GettingStarted/seleniumg5/testData/pics/SL-Flag.png";
driver.findElement(By.id("menu_pim_viewPimModule")).click();
driver.findElement(By.id("menu_pim_addEmployee")).click();
driver.findElement(By.id("firstName")).sendKeys(firstName);
driver.findElement(By.id("middleName")).sendKeys(faker.name().nameWithMiddle());
driver.findElement(By.id("lastName")).sendKeys(faker.name().lastName());
//File upload
driver.findElement(By.id("photofile")).sendKeys(profilePic);
//Employee ID
String strEmployeeID = driver.findElement(By.id("employeeId")).getAttribute("value");
Assert.assertEquals(strEmployeeID.length(), 4);
//Click check box
if (!driver.findElement(By.id("chkLogin")).isSelected()) {
driver.findElement(By.id("chkLogin")).click();
}
Select lstStatus = (new Select(driver.findElement(By.id("status"))));
lstStatus.selectByIndex(0);
//lstStatus.selectByValue("Enabled");
//lstStatus.selectByVisibleText("Enabled");
//Type username
driver.findElement(By.id("user_name")).sendKeys(firstName);
//Type password
driver.findElement(By.id("user_password")).sendKeys("Ptl@#321");
//Type re-password
driver.findElement(By.id("re_password")).sendKeys("Ptl@#321");
//Click Save button
driver.findElement(By.id("btnSave")).click();
//Verify the content
String savedFirstname = driver.findElement(By.id("personal_txtEmpFirstName")).getAttribute("value");
Assert.assertEquals(savedFirstname, firstName);
//Logout
driver.findElement(By.id("welcome")).click();
driver.findElement(By.linkText("Logout")).click();
//Verify new user login
driver.findElement(By.id("txtUsername")).sendKeys(firstName);
driver.findElement(By.id("txtPassword")).sendKeys("Ptl@#321");
driver.findElement(By.id("btnLogin")).click();
String strWelcomeMessage = driver.findElement(By.id("welcome")).getText();
Assert.assertEquals(strWelcomeMessage, "Welcome " + firstName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment