Selenium Java to Advanced Framework

Chapter 4

Launching Browser (Chrome) and Entering values

Here I will write the code for launching the browser as well as to enter values in the field and interacting with elements in the webpage.

We will be automating below website, goal is to enter a mail id , uncheck stay signed in and click on Next.

Image
package pdemo;



import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Basic2 {

	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
        
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Downloads\\chromedriver.exe");
		WebDriver driver= new ChromeDriver();			
		
       
        		
        //Launching the Site.		
        driver.get("https://login.yahoo.com/");	
        
        driver.manage().window().maximize();
        
        Thread.sleep(8000);
        
        driver.findElement(By.id("login-username")).sendKeys("[email protected]");
        Thread.sleep(3000);
        
        WebElement elem = driver.findElement(By.xpath("//*[contains(text(),'Stay signed in')]"));
        elem.click();
        
        driver.findElement(By.id("//input[@id='login-signin']")).click();

	}

}

You can see that first i have set property for Chrome browser , then launched Chrome Browser and finally hit the required url through driver.get(“url”).

First goal was to type some gmail id in the available position, i have identified locator for email id with locator ID , and passed value using keyword sendKeys . To enter values to the desired locator we uses sendKeys as in above example in Selenium. In sendKeys you have to specify what value needed to be send. Here i have send [email protected] . So written as sendKeys(“[email protected]”). Suppose if you have value to passes in sendKeys stored in any variable you can provide the variable name also>

string val =”[email protected]”;

driver.findElement(By.id(“login-username”)).sendKeys(val);

Now its replaced as sendKeys(val).

In order to click any element on webpage, you have to first identify the locator and then use click() keyword as in above example. Here first i have found out locator of Stay signed in checkbox and applied a click so that it will be unchecked.

Image

Similarly after identifying the locator for Next button , i have applied a click operation.

NOTE: In the code above after hitting URL , you can see a code “driver.manage().window().maximize();” . This is to maximize the browser you have opened through automation. If this code is not there , it will continue automation in a minimized browser.

WebElement elem = driver.findElement(By.xpath(“//*[contains(text(),’Stay signed in’)]”));
elem.click();

Above is another method of writing code, first saving the webelement locator to a variable (elem is variable name here and its return type is WebElement) and then applying desired operation on it. Here it is click operation.

Leave a comment