import java.io.File;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Testingbrowser {
//Define the Webdriver Object driver. Defining this globally can help this driver to be used in the methods in the class
WebDriver driver;
//This is the main method where we will call other methods in the test.
public static void main(String[] args) throws InterruptedException
{
//There are two part in the flow for which we created the required methods
Testingbrowser Tb = new Testingbrowser();
//provide the name of browser to run test in
Tb.LaunchBrowser("chrome");
//Call the functon to launch the application and validating the title of application
Tb.LaunchAppandValidate("https://seleniumbites.blogspot.com", "Selenium WebDriver Automation concepts");
}
//Function to launch required browser
public void LaunchBrowser (String strBrowser)
{
//Using an if else loop to launch the required browser, we will provide value from ie, chrome, and firefox.
// We can also use enum to provide the browser name in the test.
if (strBrowser.trim().toLowerCase().contentEquals("ie"))
{
//Provide the file path for IE Server
File file = new File("D:\\Selenium_Automation\\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"))
{
//Provide the file path for Chrome drive
System.setProperty("webdriver.chrome.driver", "D:\\Selenium_Automation\\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 name in not from the list, print the below message
System.out.println("Please select a browser from IE/Firefox or chrome");
}
}
//Function to perform required action on the application
public void LaunchAppandValidate(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());
}
}
}
No comments:
Post a Comment