As in our previous lecture, we have discussed the basic configuration of the selenium web driver. In this article, we will discuss how to write the first script in Java using selenium web driver. let us try to create a WebDriver script of the following test scenario:
Give Username
Click on Next button
Give Password
Click on the Next button
Below is the selenium web driver code for the above scenario.
In this example we have used the chrome browser, so we have downloaded a chrome driver. In the SetProrpty method, we need to give two parameters one is "value" and the other is "key".
The following are the two parameters for this method. value: webdriver.chrome.driver Key: chrome driver path on your local system, where you have placed the executable.
Test Scenario:
Open this URL https://www.gmail.com/Give Username
Click on Next button
Give Password
Click on the Next button
Below is the selenium web driver code for the above scenario.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class GmailLoginScript { public static void main(String[] args) { // First we need to set path for chromeDriver //System.setProperty("webdriver.gecko.driver","/home/khawer/Desktop/geckodriver"); System.setProperty("webdriver.chrome.driver","/home/khawer/Desktop/chromedriver"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().deleteAllCookies(); driver.get("https://gmail.com"); // UserName is given wrong deliberately. You can use your valid UserName. WebElement username = driver.findElement(By.xpath("//input[@type= 'email']")); username.sendKeys("test@gmail.com"); WebElement nextBtn = driver.findElement(By.xpath("//span[text()= 'Next']")); nextBtn.click(); // Password is given wrong deliberately. You can use your valid Password. try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } WebElement password = driver.findElement(By.xpath("//input[@type = 'password']")); password.sendKeys("abc123"); driver.findElement(By.xpath("//div[@id = 'passwordNext']")).click(); // close Firefox driver.close(); } }Now let's try to explore the above script written for Gmail login.
Property Settings:
Before going into detail of this line of code first, we need to understand one thing that is browser driver. Every browser has a browser driver like FirefoxDriver, ChromeDriver, etc. You can download the respective browser driver that you are going to use in your application. For the Firefox browser, we need to download a gecko driver.In this example we have used the chrome browser, so we have downloaded a chrome driver. In the SetProrpty method, we need to give two parameters one is "value" and the other is "key".
The following are the two parameters for this method. value: webdriver.chrome.driver Key: chrome driver path on your local system, where you have placed the executable.
System.setProperty("webdriver.chrome.driver","/home/khawer/Desktop/chromedriver");
Note: Compatibility is a major issue while using Firefox. For Firefox 35, you should use a gecko driver created by Mozilla to use Web Driver. The use of Selenium 3.0, gecko, and firefox has compatibility issues, and setting them correctly could become an uphill task. In case of any issue, downgrade to Firefox version 47 or below. But for on Chrome things are much easy. Selenium works out of the box for Chrome. Without a Chrome Driver, we can't instantiate the Chrome browser.
If we run the program without setting the system property, the system will through the following exception. "IllegalStateException"
Object and variables Initialization:
Normally, this is how a driver object is created. Actually, "driver" is a reference variable of the WebDriver interface and an object of ChromeDriver class is assigned to it. It means ChromeDriver class will implement all methods of WebDriver interface.WebDriver driver = new ChromeDriver();
Clear Cookies & Maximize Window:
The following two-line is used to clear the cookies before launching the browser. This line of code is not mandatory, but for better performance, we can use it.driver.manage().deleteAllCookies();This command is used to maximize the browser window.
driver.manage().window().maximize();
Starting Browser Session:
In Selenium Web Driver get() method is used to launch a new browser session and directs it to the URL that you specify as its parameter. The following statement will launch a browser and open the Gmail login page.driver.get("https://gmail.com");
Finding UI Elements:
To find the UI elements different locators are used in Selenium WebDriver. You can find UI elements by XPath, ID, Name, Class, CSSSelectors, LinkText, etc. We will discuss it in full detail in the upcoming lecture. Here we are using findelement() method of WebDriver interface to find the particular element.A reference variable of the Web Element interface is used to store the particular element. Basically, the WebElement interface is responsible to interact with web elements like sendkeys(), click(), etc.
WebElement username = driver.findElement(By.xpath("//input[@type= 'email']")); username.sendKeys("test@gmail.com"); WebElement nextBtn = driver.findElement(By.xpath("//span[text()= 'Next']"));
You can also write it in a simple way. For understanding the purpose above statements is good.
driver.findElement(By.xpath("//input[@type='email']")).sendKeys("test@gmail.com");
Clicking on an Element:
The Click() method is used to click on any web element.nextBtn.click();The following lines of code are just a way of making selenium WebDriver to wait, until the next element loads. There are two types of wait in Selenium WebDriver that we will discuss in detail in a separate article.
A brief introduction to wait is:
Implicit wait - A default waiting time that is used throughout the program
Explicit wait - It is used to wait for a particular instance only.
In this article, we are just giving a basic introduction to Selenium WebDriver. In some situations, without using the wait script not works properly. An exception raised, "NoSuchelementFound". So it is necessary to use the wait in Selenium WebDriver where elements are not loaded accurately.
try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }Similarly, we can find the password field by using the findelement() method.
WebElement password = driver.findElement(By.xpath("//input[@type = 'password']")); password.sendKeys("abc123");To click on the sign-in button the following line of code will work.
driver.findElement(By.xpath("//div[@id = 'passwordNext']")).click();
Close Browser Session:
The close() method is used to close the browser that WebDriver is currently controlling.driver.close();It closes all the windows that WebDriver has opened.
driver.quit();Terminating the Entire Program: This command will close your Java program without closing the window.
System.exit(0);
0 comments:
Post a Comment