Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Selenium Webdriver Execution

So far, in selenium automation, we have seen what is web driver and its architecture. Now its time execute the scripts using Web driver.

Aim : 

To verify the user required URL and display the title as an output

working with Firefox browser:

Create a JAVA project "Selenium  tutorial"

and likewise, Create a Package " selenium test"

Create a JAVA class " Selenium  driver example"

And Execute the following Code in your IDE

package selenium test;

//Importing packages//We need to import relevant packages depends on our needs.import org.openqa.selenium.WebDriver;//It contains the WebDriver class to instantiate a new browserimport org.openqa.selenium.firefox.FirefoxDriver;//It contains the FirefoxDriver class to instantiate a Firefox driverpublic class Seleniumdriverexample {public static void main(String[] args) {//Instantiation of driver object. To launch Firefox browserWebDriver driver = new FirefoxDriver();//Declaration of variablesString url = "https://www.onlineitguru.com";//Expected titleString expectedTitle = " Online IT Certification Training | Online Courses | OnlineITGuru";String actualTitle = null;//To oepn URL "https://onlineitguru.com/onlinetutorials/selenium". This is what we have assigned to the variable named 'url'.driver.get(url);//To get the actual value of the title. getTitle method used to get the page titleactualTitle = driver.getTitle();//Using if-else condition to compare the Expected Title and Actual Title. As per the below lines of code (if-else condition).if (actualTitle.contentEquals(expectedTitle)){//'system.out.println' prints the outputSystem.out.println("Expected Value is "+expectedTitle);System.out.println("Actual Value is "+actualTitle);System.out.println("Test Passed");} else {System.out.println("Expected Value is "+expectedTitle);System.out.println("Actual Value is "+actualTitle);System.out.println("Test Failed");}//'close' method is used to close the browser windowdriver.close();//To run the script - Go to menu bar - click on Run - Run or use shortcut key Ctrl+F11//You could see the output in the console as shown below://Test Passed}}
working with Chrome Driver:

Initially ,  to work with the chrome driver, we need  to download and install the Chrome driver. You can easily download the chrome files at https://sites.google.com/a/chromium.org/chromedriver/downloads

Once you visit the above link, you will be finding several links to download the required drivers. Check your local system chrome version and download the respective chrome drivers. Once downloaded, the downloaded file will be in ZIP format.  Then unzip the file and save it somewhere in your local system. Then execute the below code, to open the required URL  through the Chrome browser.

package seleniumTutorial;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;public class ChormeBrowserScript {public static void main(String [] args) throws InterruptedException{//System.setProperty("webdriver.chrome.driver",path of executable file "Chromedriver.exe")System.setProperty("webdriver.chrome.driver", "D:/SeleniumEnvironment/chromedriver_win32/chromedriver.exe");WebDriver driver = new ChromeDriver();driver.get("https://www.onlineitguru.com");System.out.println("Online IT Certification Training | Online Courses | OnlineITGuru");driver.close();}}

Get more real time examples on these from selenium online training

Working with Internet Explorer:

Like Firefox and Chrome. this Internet Explorer has separate drivers to test the URLs. You can find the Internet explorer drivers at https://www.seleniumhq.org/download/

Here, you will be finding different links to download the driver. So download the driver according to your local system. And unzip the files and save it somewhere in your local system. Then execute the following code  Eclipse

package rules;

import org.openqa.selenium.WebDriver;import org.openqa.selenium.ie.InternetExplorerDriver;public class stmtest {public static void main(String [] args) throws InterruptedException{//System.setProperty("webdriver.ie.driver",path of executable file "IEDriverServer.exe")System.setProperty("webdriver.ie.driver", "D://Selenium Environment//IEDriverServer_x64_2.53.1//IEDriverServer.exe");//Initialize InternetExplorerDriver Instance.WebDriver driver = new InternetExplorerDriver();driver.get("https://www.onlineitguru.com");System.out.println("Online IT Certification Training | Online Courses | OnlineITGuru");driver.close();}}

 Also Check how to handle Iframes in Selenium Webdriver