Selenium Webdriver Execution

Click to rate this post!
[Total: 0 Average: 0]

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
  • Microsoft Azure Architect Technologies(AZ-300) | ITGuru
    Microsoft Azure Architect Technologies(AZ-300)
  • Azure is a great Microsoft Cloud Computing platform in providing various cloud services through online. ITGuru Certified Azure Architect certification course gives you the practical knowledge on Azure Cloud platform through real-world use cases from live experts

  • ServiceNow Admin Training | ITGuru
    ServiceNow Admin Training
  • Getting knowledge of cloud platforms like ServiceNow is essential in today’s world for the smooth running of projects in cloud platform. Turn your dream to the reality of becoming the Certified ServiceNow Administrator through ServiceNow Administration online certification Course with practical examples by live industry experts through online at ITGuru with real-world use cases.

  • Workday Financials Training | ITGuru
    Workday Financials Training
  • knowing the basics on any platform like Workday is not enough to sustain the IT industry. Hence it is essential to go beyond on Workday basics like Workday Financials training which lets you know the application of Financials management in real -world use cases from ITGuru Live Experts in a practical way.

  • Workday Training
  • An organization is considered as the best one when it offers the best benefits to the employee. Moreover, the greater the employee benefits, the greater the contribution to the organization. ITGuru let you know the practical workday Human Resource Management(HRM) features with live examples by experts

  • Python Online Course | ITGuru
    Python Online Course
  • Turn your dream into reality by ITGuru live experts with real-world use cases through practical knowledge on python online course and become the certified associate in python programming and become a master in python programming

  • Python Programming | ITGuru
    Python Programming
  • Python is the trending programming language in the IT industry. Mastering in python programming gives you more value among the people in the IT industry. Hence start today to learn python programming online by live experts with real-time uses cases at ITGuru


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

Call Now Button