Monday, July 27, 2020

Selenium testing in JAVA

Following JAVA code can be used to test web application with login page, filtering data and submit form.

You have to install web browser driver. I used chrome driver.

pom.xml

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
 
JAVA code


System.setProperty("webdriver.chrome.driver","./driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();

String baseUrl = "https://testwebsite/login";

driver.get(baseUrl);

//maximizing window

driver.manage().window().maximize();

//get login form details

WebElement username = driver.findElement(By.name("username"));
WebElement password = driver.findElement(By.name("password"));
WebElement login = driver.findElement(By.xpath("//input[@value='Login']"));

// login to site

username.sendKeys("admin");
password.sendKeys("admin@123");
login.click();

// filtering using placeholder

WebElement fullName = driver.findElement(By.xpath("//input[@placeholder='Filter Full Name']"));
WebElement country = driver.findElement(By.xpath("//input[@placeholder='Filter Country']"));
WebElement filter = driver.findElement(By.xpath("//input[@value='Filter']"));

// filter sujith & Sri Lanka

fullName.sendKeys("Sujith");
country.sendKeys("Sri Lanka");
filter.click();

// add new record to list 

WebElement add = driver.findElement(By.xpath("//*[text()='Add New']"));
add.click();

//filter by name

WebElement frmFullName = driver.findElement(By.name("fullName"));
WebElement frmAge = driver.findElement(By.name("age"));
WebElement frmAddress = driver.findElement(By.name("address"));
//filter by placeholder
WebElement frmBirthPicker = 
driver.findElement(By.xpath("//input[@placeholder='yyyy-mm-dd']"));
WebElement frmCountry = driver.findElement(By.name("country"));
//filter by parent input with 'GIT' label
WebElement frmGIT = driver.findElement(By.xpath("//label[contains(., 'GIT')]/parent::*//input"));
WebElement frmHTML = driver.findElement(By.xpath("//label[contains(., 'HTML')]/parent::*//input"));
WebElement frmMale = driver.findElement(By.id("Male"));

frmFullName.sendKeys("Test full name");

frmAge.sendKeys("24");
frmAddress.sendKeys("Test address1");
frmBirthPicker.sendKeys("2010-10-10");
frmCountry.sendKeys("Sri Lanka");
//checking buttons
frmGIT.click();
frmHTML.click();
frmMale.click();

//submitting form  

WebElement submit = driver.findElement(By.xpath("//input[@value='Submit']"));
submit.click();
    
// driver.close();

No comments:

Post a Comment