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 browser
import org.openqa.selenium.firefox.FirefoxDriver;
//It contains the FirefoxDriver class to instantiate a Firefox driver

public class Seleniumdriverexample {

public static void main(String[] args) {

//Instantiation of driver object. To launch Firefox browser
WebDriver driver = new FirefoxDriver();
//Declaration of variables
String url = "https://www.onlineitguru.com";

//Expected title
String 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 title
actualTitle = 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 output
System.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 window
driver.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

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • Deep learning course
    Deep Learning Course
  • Artificial Intelligence, Deep mastering (DL) is completely about, many levels of Representation and sort of abstraction. That guide to design a sense of Information like Images, sound and text format.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.


100% Secure Payments. All major credit & debit cards accepted.