Weekend -Special offer upto 50% off | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Webdriver Operators
Browser Elements Operations :

Selenium supports different kinds of operations. So by adding packages to your project,  this tools allows you to perform various operations. So let us have looked over the various operations that the selenium tool can perform.

import org.openqa.selenium.WebDriver;

This is a library package that contains the required class to initiate the browser loaded with a specific driver.

import org.openqa.selenium.firefox.FirefoxDriver;

This library package contains the Firefox Driver Class. When the driver class initiated, this class starts the driver as a class.

System.setProperty(“webdriver.gecko.driver”, “files/geckodriver.exe”);

This command notifies the run time engine that the Gecko driver is present in the specified path. So to work with the web driver after Firefox 35, we need to download the Gecko driver.  Besides,  if you want to test it on chrome, you have to download on the chrome driver and specify its path in its line of code.  Moreover,  online courses added, this procedure is similar for other browsers too.

WebDriver driver = new FirefoxDriver();

This command is used to initiate the new FireFox driver object.

driver.get(“https://onlineitguru.com/”);

This method is used to open a Specified URL.

driver.getTitle(); –

This command gets the title tab that is currently open in the browser.

driver.quit();

This command closes the driver

navigate.to()

This is used to navigate to other URL and do testing.

navigate.back()

This is used to come back to the previous page.

navigate.refresh():

This is used to refresh the current page.

We can maximize the browser with the following code

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

Today in this computerized world, we can find a person without the wallet, but we could not find people without mobile ( I hope asking you regarding the Facebook account is Stupid ! ) So let's have a look regarding how to login the Facebook account through Code.

Visit www.facebook.com and right click in that area and select Inspect element and finally move to the Elements column. In that are you will be finding different options like E-mail/phone, Password, and log in.  And through code, you can add these fields in the following manner.

driver.findElement(By.name("email")).sendKeys("xxx@gmail.com");driver.findElement(By.name("pass")).sendKeys("xxxxxx");driver.findElement(By.id("u_0_q")).click();

So adding these line of code is not enough.  Because the dynamics of the page may (or) may not respond fastly. And if the page does not respond fastly, it throws a timeout exception error. And this issue does not happen on popular websites like Facebook. But this usually happens in e-commerce websites

So we need an alternative solution to this problem. Moreover, here we need to request the web driver to wait sometime when the page is accessed. And if the page load completely, we need to locate the elements and then perform the actions. So in this case, you want your web driver to wait until all the elements in the web page get loaded and then closes the browser. So in this case, you use the driver.wait() and thread.sleep ().  Moreover, this method allows you to wait for the specified amount of time by the user. 

The following code opens the Facebook ID and password ( Replace your credentials in your code)

Code:
package seleniumWebDriver;import org.openqa.selenium.WebDriver;//import firefoximport org.openqa.selenium.firefox.FirefoxDriver;import java.util.concurrent.TimeUnit;public class WebDriverClass{public static void main(String[] args){System.setProperty("webdriver.gecko.driver", "files/geckodriver.exe");WebDriver driver = new FirefoxDriver();driver.get("https://www.facebook.com/");driver.manage().window().maximize();driver.getTitle();driver.navigate().to(“https://www.edureka.co/testing-with-selenium-webdriver”);driver.navigate().back();driver.navigate().refresh();driver.wait(5000);// or use// Thread.sleep(5000);driver.findElement(By.name("email")).sendKeys("xxx@gmail.com");driver.findElement(By.name("pass")).sendKeys("xxxxxx");driver.findElement(By.id("u_0_q")).click();driver.quit();}}

Also check How to integrate Selenium Webdriver with Cucumber