In this article, we will discuss on how we launch Selenium web driver in different browsers in Java.
Before we go further with this, We need installing different jar files and making initial set up in Java, Please go through below link and other useful links to set up initial java project in eclipse with selenium jar files added to project build path.
Once we have configured eclipse and added required external
libraries in the build path, we can start with a basic test in eclipse to solve
following requirement:
- Open www.sagenda.net on browser specified by user
- Verify the title of the Page has Sagenda in the text.
- Close the browser.
Below is the self explanatory code in java using Selenium to perform the required job:
//These are the class libraries that needs to be imported
import java.io.File;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Iteration {
//Define Webdriver object
WebDriver driver;
//This is the main method to drive the test flow
public static void main(String[] args) throws InterruptedException
{
//There are two part in the flow for which we created the required methods
Iteration iter = new Iteration();
//a. Launching the required Browser
iter.LaunchBrowser("chrome");
//b. Providing the url of application and condition to verified in the test. Text in title in this example
iter.homePage("https://sagenda.net", "Sagenda");
}
//Function to launch required browser
public void LaunchBrowser (String strBrowser)
{
//based on the expected browser as provided in argument of method, we will launch the required browser
if (strBrowser.trim().toLowerCase().contentEquals("ie"))
{
//Launch IE browser://Pre-Conditions: Download IE driverServer.exe from the selenium download site and provide location where it is downloaded
File file = new File("D:\\Selenium\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
}
else if (strBrowser.trim().toLowerCase().contentEquals("firefox"))
{
driver = new FirefoxDriver();
}
else if (strBrowser.trim().toLowerCase().contentEquals("chrome"))
{
//Pre-Conditions: Download IE driverServer.exe from the selenium download site and provide location where it is downloaded
System.setProperty("webdriver.chrome.driver", "d://chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
//Provide location where chrome is currently installed.
String Chrome_Path = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
capabilities.setCapability("chrome.binary",Chrome_Path);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
}
else
{
//In case browser is not in above three , print below message
System.out.println("This browser is currently not supported by me, Please select from IE/Firefox or chrome");
}
}
//Function to perform required action on the application
public void homePage(String url, String strTextinTitle)
{
try
{
//open the required url
driver.get(url);
//once the url is launched, verify the title contains the required text.
//Note : In case we require exact match , we will use contentequals instead of contains
if (driver.getTitle().contains(strTextinTitle))
{
System.out.println("Title of the Page contains text :" + strTextinTitle );
}
else
{
System.out.println("Title of the Page does not contains text :" + strTextinTitle );
}
}
catch(Exception e)
{
System.out.println(e.getClass());
}
}
}