`
public class CucumberHooks {
public static void main(String[] args) {
}
protected static WebDriver driver;
#Before
public void setup() throws IOException {
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("extension_5_3_2_0.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
String browser = ReadPropertyFileSingleton.getInstance().getProp("browser");
driver = Util.setEnvironmentAndGetDriver(browser);
assert driver != null;
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public static WebDriver getDriver() {
return driver;
}
#After
public void close() {
driver.quit();
}}
The code where I added adblock extensions via crx file
When I run tests through a feature file, a regular browser without a blocker is launched
the browser is also launched already with a blocker, but it does not see the steps
what could be the problem and is it possible to use adblock this way?
Related
public class LaunchBrowser {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
System.setProperty("webdriver.gecko.driver", "/Users/spectra/Drivers/geckodriver");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
driver.get("https://www.seleniumhq.org");
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
FirefoxDriver cannot be resolved to a type
at com.simplilearn.day2.oops.LaunchBrowser.main(LaunchBrowser.java:16)
setProperty before driver initialization which should be like
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "/Users/spectra/Drivers/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
driver.get("https://www.seleniumhq.org");
}
My test is executed on my VM. Chrome loads first and then IE. IE Completes the test but Chrome gets orphaned. I think this has something to do with the threads connecting with the proper browser.
I have tried many hours trying different ways of setting up Parallel testing with Selenium/TestNG and the result is the same as I described above.
My goal is for both browsers to complete the test. Can you please help me.
Please find my code below.
public class BaseTestDirectory {
// -------Reference Variables-------------
// ----- Regression Test Cases -----
LoginLogoutPage objBELogin;
HomeNavigationPage obj_navigation;
DirectoryPage obj_directory;
// -------------------------------
protected static WebDriver driver;
protected ExtentTest test;// --parent test
ExtentReports report;
ExtentTest childTest;
#BeforeClass
#Parameters(value={"browser"})
public void setUp(String browser) throws InterruptedException, MalformedURLException {
// --------Extent Report--------
if(browser.equals("Chrome")){
report = ExtentManager.getInstance();
System.setProperty("webdriver.chrome.driver", "C:\\GRID\\chromedriver.exe");
System.out.println(System.getenv("BUILD_NUMBER"));
String env = System.getProperty("BUILD_NUMBER");
if (env == null) {
driver = new RemoteWebDriver(new URL(COMPLETE_NODE_URL), OptionsManager.getChromeOptions());
driver.get(HOME_PAGE);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
} else {
driver = new ChromeDriver();
driver.get(HOME_PAGE);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
else if (browser.equals("IE")) {
report = ExtentManager.getInstance_IE();
System.setProperty("webdriver.ie.driver", "C:\\GRID\\IEDriverServer.exe");
System.out.println(System.getenv("BUILD_NUMBER"));
String env = System.getProperty("BUILD_NUMBER");
driver = new RemoteWebDriver(new URL(COMPLETE_NODE_URL), OptionsManager.getInternetExplorerOptions());
driver.get(HOME_PAGE);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
public class OptionsManager {
//Get Chrome Options
// --https://stackoverflow.com/questions/43143014/chrome-is-being-controlled-by-automated-test-software
// --https://stackoverflow.com/questions/56311000/how-can-i-disable-save-password-popup-in-selenium
public static ChromeOptions getChromeOptions() {
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.addArguments("--disable-features=VizDisplayCompositor");
options.addArguments("--start-maximized");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
capability.setPlatform(Platform.XP);
capability.setBrowserName("Chrome");
capability.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(capability);
return options;
}
public static InternetExplorerOptions getInternetExplorerOptions () {
InternetExplorerOptions capabilities = new InternetExplorerOptions();
capabilities.ignoreZoomSettings();
capabilities.setCapability("browser.download.folderList", 2);
capabilities.setCapability("browser.download.manager.showWhenStarting", false);
capabilities.setCapability("browser.helperApps.neverAsk.saveToDisk","application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;");
capabilities.setCapability("browser.helperApps.alwaysAsk.force", false);
capabilities.setCapability("browser.download.manager.alertOnEXEOpen", false);
capabilities.setCapability("browser.download.manager.focusWhenStarting", false);
capabilities.setCapability("browser.download.manager.useWindow", false);
capabilities.setCapability("browser.download.manager.showAlertOnComplete", false);
capabilities.setCapability("browser.download.manager.closeWhenDone", false);
//capabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
//capabilities.setCapability("requireWindowFocus", true);
return capabilities;
}
}
Initializing the WebDriver object as a Thread Local for Parallel Test Execution: When you have decided to run your selenium's tests in parallel, your Webdriver object should be thread-safe i.e. a single object can be used with multiple threads at the same time without causing problems.
public class BaseTest {
//Declare ThreadLocal Driver (ThreadLocalMap) for ThreadSafe Tests
protected static ThreadLocal<RemoteWebDriver> driver = new ThreadLocal<>();
public CapabilityFactory capabilityFactory = new CapabilityFactory();
#BeforeMethod
#Parameters(value={"browser"})
public void setup (String browser) throws MalformedURLException {
//Set Browser to ThreadLocalMap
driver.set(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilityFactory.getCapabilities(browser)));
}
public WebDriver getDriver() {
//Get driver from ThreadLocalMap
return driver.get();
}
#AfterMethod
public void tearDown() {
getDriver().quit();
}
#AfterClass void terminate () {
//Remove the ThreadLocalMap element
driver.remove();
}
}
Try to design your base class in the below given example
public class WebDriverFactory {
static WebDriver create(String type) throws IllegalAccessException{
WebDriver driver;
switch(type) {
case "Firefox":
driver = new FirefoxDriver();
break;
case "Chrome":
driver = new ChromeDriver();
break;
default:
throw new IllegalAccessException();
}
return driver;
}
}
public class BaseClass extends WebDriverFactory {
public static ThreadLocal<WebDriver> dr = new ThreadLocal<WebDriver>();
#Parameters("browser")
#BeforeMethod
public void beforemethod() throws IllegalAccessException{
WebDriver driver = new WebDriverFactory().create(browser);
setWebDriver(driver);
getWebDriver().manage().window().maximize();
getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
public void setWebDriver(WebDriver driver){
dr.set(driver);
}
public WebDriver getWebDriver(){
return dr.get();
}
#AfterMethod
public void aftermethod(){
getWebDriver().quit();
dr.set(null);
}
}
When i trying to execute then every time an error message was occur.
Unable to create new remote session.desired capabilities = Capabilities [{marionette=true, firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions#1660f4e, browserName=firefox,moz:firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions#1660f4e, version=, platform=ANY}], required capabilities = Capabilities [{}]
Code trials:
package com.rsmpl.hometask;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class ComboValue {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "E:\\Arijit_Backup\\Utility\\Java_setup\\geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
FirefoxDriver fd = new FirefoxDriver(capabilities);
fd.get("http://74.50.58.66/jkhealth_test");
Thread.sleep(10000);
}
}
Are you using an admin account on your workstation? This might cause the issue.
Try specifying a path to Firefox in your capabilities:
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "E:\\Arijit_Backup\\Utility\\Java_setup\\geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
capabilities.setCapability("firefox_binary","pathToYourFirefoxExecutable\\Mozilla Firefox\\firefox.exe");
FirefoxDriver fd = new FirefoxDriver(capabilities);
fd.get("http://74.50.58.66/jkhealth_test");
Thread.sleep(10000);
}
Why is Gecko Driver (v0.17.0 - x64bit) not opening Browser?
Base Page / Method:
public BasePage loadUrl(String url) throws Exception {
driver.get(url);
return new BasePage(driver);
}
Cucumber Step:
#Given("^User navigates to the \"([^\"]*)\" website$")
public void user_navigates_to_the_website(String url) throws Throwable {
BasePage basePage = new BasePage(driver);
basePage.loadUrl(url);
}
Driver Factory:
public WebDriver getDriver() {
try {
if(driver == null){
System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
basePage = PageFactory.initElements(driver, BasePage.class);
loginPage = PageFactory.initElements(driver, LoginPage.class);
}
} catch (Exception e) {
}
return driver;
}
NEW CODE - Driver Factory: uses if statements to point to exe files for each browser type:
public WebDriver getDriver() {
try {
ReadConfigFile file = new ReadConfigFile();
if (driver == null) {
if("chrome".equalsIgnoreCase(file.getBrowser())){
System.setProperty("webdriver.chrome.driver", Constant.CHROME_DRIVER_DIRECTORY);
driver = new ChromeDriver();
}
if("firefox".equalsIgnoreCase(file.getBrowser())){
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
System.setProperty("webdriver.gecko.driver", Constant.GECKO_DRIVER_DIRECTORY);
capabilities.setCapability("marionette", true);
driver = new FirefoxDriver(capabilities);
}
if("ie".equalsIgnoreCase(file.getBrowser())){
System.setProperty("webdriver.ie.driver", Constant.IE_DRIVER_DIRECTORY);
driver = new InternetExplorerDriver();
}
}
}
fixed upgrading to Selenium version: 3.4.0
I had tried to run below JUnit (Selenium WebDriver) test case to open Google in Chrome browser, but it is failing with error message as
"The path to the ChromeDriver executable must be set by the
webdriver.chrome.driver system property; for more information, see
http://code.google.com/p/selenium/wiki/ChromeDriver."
As specified in that website, I downloaded ChromeDriver.exe but don't know,
Which PATH should I place that? or
How to set ChromeDriver path in webdriver.chrome.driver?
Please Advise.
My JUnit test case (changed the Firefox Driver to Chrome Driver):
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
public class Chrome_Open_Google {
private WebDriver driver;
private String baseUrl;
#Test
public void Test_Google_Chrome() throws Exception {
driver = new ChromeDriver();
baseUrl = "http://www.google.co.uk/";
driver.get(baseUrl);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
I believe you have several options:
Either specify the folder (in which your chromedriver binary is) in your PATH system variable - here's how
Or give you application webdriver.chrome.driver as a system property by calling it with -Dwebdriver.chrome.driver=the/path/to/it parameter.
Or the same programatically: System.setProperty("webdriver.chrome.driver", "your/path/to/it");
Or this:
private static ChromeDriverService service;
private WebDriver driver;
#BeforeClass
public static void createAndStartService() {
service = new ChromeDriverService.Builder()
.usingChromeDriverExecutable(new File("path/to/my/chromedriver"))
.usingAnyFreePort()
.build();
service.start();
}
#Before
public void createDriver() {
driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
}
#After
public void tearDown() throws Exception {
driver.quit();
}
#AfterClass
public static void createAndStopService() {
service.stop();
}
System.setProperty("webdriver.chrome.driver", "your\path\to\it");
For Eg :
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\driver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();