Created
December 29, 2018 14:44
-
-
Save pragmatictesters/3dcdbeee17bbf1d9d1ecb656046a3ead to your computer and use it in GitHub Desktop.
Selenium WebDriver Examples : Working with basic web elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
/** | |
* Demonstrate use of basic web elements (input boxes, buttons, file upload , checkboxe , links and drop down ) | |
* Creating dynamic test data using com.github.javafaker.Faker is demonstrated | |
* TestNG Assert is used for verifications, organizing the tests and setting the order of execution | |
**/ | |
public class AddEmployeeTest { | |
private static final String PASSWORD = "Ptl@#321"; | |
private static final String BASE_URL = "http://hrm.pragmatictestlabs.com"; | |
private static final String USERNAME = "Admin"; | |
private WebDriver driver; | |
private Faker faker; | |
private String firstName; | |
private String profilePic = "/Users/hansi/Desktop/playground/Ex_Files_Java_SE_8_NF/Exercise Files/01_GettingStarted/seleniumg5/testData/pics/SL-Flag.png"; | |
@BeforeClass | |
public void beforeClass() { | |
WebDriverManager.chromedriver().setup(); | |
faker = new Faker(); | |
} | |
/** | |
* Launching a browser with inforbar disable and maximized | |
* Set the implicit wait | |
*/ | |
@BeforeMethod | |
public void beforeMethod() { | |
ChromeOptions options = new ChromeOptions(); | |
options.addArguments("disable-infobars"); | |
driver = new ChromeDriver(options); | |
driver.manage().window().maximize(); | |
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); | |
driver.navigate().to(BASE_URL); | |
} | |
/** | |
* Closing the browser | |
*/ | |
@AfterMethod | |
public void afterMethod() { | |
driver.close(); | |
} | |
@Test | |
public void testAddEmployees() { | |
driver.findElement(By.id("txtUsername")).sendKeys(USERNAME); | |
driver.findElement(By.id("txtPassword")).sendKeys(PASSWORD); | |
driver.findElement(By.id("btnLogin")).click(); | |
firstName = "User" + faker.name().firstName(); | |
//Menu and sub menu navigation | |
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 using sendKeys method | |
//Note : This may not work sometimes. We will demonstrate use of third-party tools to demonstrate this | |
driver.findElement(By.id("photofile")).sendKeys(profilePic); | |
//Employee ID | |
String strEmployeeID = driver.findElement(By.id("employeeId")).getAttribute("value"); | |
Assert.assertEquals(strEmployeeID.length(), 4); | |
//Working with a check box | |
//Ensure the check box is ticked. | |
if (!driver.findElement(By.id("chkLogin")).isSelected()) { | |
driver.findElement(By.id("chkLogin")).click(); | |
} | |
//Selecting an item from a dropdown | |
Select lstStatus = (new Select(driver.findElement(By.id("status")))); | |
lstStatus.selectByIndex(0); //Select by the index. It is zero based | |
//lstStatus.selectByValue("Enabled"); //Select an element by value attribute | |
//lstStatus.selectByVisibleText("Enabled"); //Select an element by visible text | |
//Type username | |
driver.findElement(By.id("user_name")).sendKeys(firstName); | |
//Type password | |
driver.findElement(By.id("user_password")).sendKeys(PASSWORD); | |
//Type re-password | |
driver.findElement(By.id("re_password")).sendKeys(PASSWORD); | |
//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(); | |
//Click Logout link | |
driver.findElement(By.linkText("Logout")).click(); | |
} | |
/** | |
* Login with the newly created user credentials | |
* dependsOnMethods = {"testAddEmployees"} will ensure testAddEmployee method is executed prior to this test | |
*/ | |
@Test(dependsOnMethods = {"testAddEmployees"}) | |
public void loginWithNewUserCredentials() { | |
//Verify new user login | |
driver.findElement(By.id("txtUsername")).sendKeys(firstName); | |
driver.findElement(By.id("txtPassword")).sendKeys(PASSWORD); | |
driver.findElement(By.id("btnLogin")).click(); | |
//Verify the welcome message for the new user | |
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