How to setup or run automation scripts on chrome browser for Selenium automation testing
Here I am giving some example to control browsers from config.properties file as well.
Step 1:Create a folder name config under your project
Step 2: Create a file name call it as config.properties file
Step 3: paste this
#### Control browsers from here 'Firefox, Chrome, Internet Explorer’ whenever you want to change browser you can change here directly.
BROWSER=chrome
Step 4:
Download Chrome driver from this page
or directly from here
Create a folder in your selenium project as Chrome driver and place this Chrome .exe file in that folder
And now right click on that .exe take path.
Example path like this : projectname//src//chromedriver//chromedriver
Step 5: Create a java file as below
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class DriverStart
{
public static WebDriver driver;
public static String BROWSER = null;
public static String USERDIR = System.getProperty("user.dir”);
public static ResourceBundle bundle = null;
public static InputStream initPropertyFile = null;
public static String PROPERTY_PATH = null;
public JavascriptExecutor jse;
PROPERTY_PATH = USERDIR
+ "/Config/config.properties";
if (initPropertyFile == null) {
initPropertyFile = new FileInputStream(PROPERTY_PATH);
}
if (bundle == null) {
bundle = new PropertyResourceBundle(initPropertyFile);
}
BROWSER = bundle.getString("BROWSER");
if (BROWSER.equalsIgnoreCase("chrome")) {
File file = new File(“Chrome drive .exe file path");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
builder = new Actions(driver);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS );
jse = (JavascriptExecutor) driver;
}
|
|
// Here you can call Firefox..
if (BROWSER.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
builder = new Actions(driver);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS );
jse = (JavascriptExecutor) driver;
}
}
No comments:
Post a Comment