Selenium (chromedriver) in MAC - selenium

I have the following problem/question:
I was with notebook automation. Now I bought an ultrabook MAC.
I did all the project import with automation, all right. However, when I run it, I get the following error:
java.lang.IllegalStateException: The driver executable does not exist: /Users/estevaomarcos/Documents/Projetos/DBServer/\Users\chromedriver
In my code is the following:
System.setProperty("webdriver.chrome.driver", "\\Users\\chromedriver");
WebDriver browser = new ChromeDriver(); .....

Set Driver Path: To avoid manual checking for the operating system we can get the file separator symbol from the system property using the file.separator key.
System.setProperty("webdriver.chrome.driver", "Users" + System.getProperty("file.separator") + "chromedriver");
WebDriver browser = new ChromeDriver();
browser.get("test.com");
Setting driver path based on which Operating System(OS) we are running on.
String os = System.getProperty("os.name").toLowerCase();
WebDriver driver = new ChromeDriver();
if(os.contains("mac")) {
System.setProperty("webdriver.chrome.driver", System.getProperty("usr.home") + System.getProperty("file.separator") + "chromedriver");
}else {
System.setProperty("webdriver.chrome.driver", System.getProperty("usr.home") + System.getProperty("file.separator") + "chromedriver.exe");
}

In a code based on operating system you should initialise new ChromeDriver after System.setProperty setup only.
user.home is correct option - NOT "usr"
Finally it works(java) great when looks like this:
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("mac")) {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.home") + System.getProperty("file.separator") + "chromedriver");
} else {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.home") + System.getProperty("file.separator") + "chromedriver.exe");
}
WebDriver driver = new ChromeDriver();

Related

How to test Electron app using selenium and java

Hi Im having an issue with testing an electron app. Up until last week our product was ran on chrome. But now the product has been changed to an electron desktop app and when launched the window isnt picked up.
The flow is basically I open the product on chrome and it appears as a pop up. Previously this was just a chrome pop up but now its an electron app. And now i cnat seem to switch to this window. Im wondering is it possible to switch between the two or do i need a different driver and just test he electron app by itself?
My driver factory is shown here
public class DriverFactory {
private static WebDriver driver;
public static WebDriver startDriver() {
String projectLocation = System.getProperty("user.dir");
// add in elements for logging into the mobile application also - Android and
// iOS.
if (OSValidator.isMac()) {
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver_mac");
} else if (OSValidator.isWindows()) {
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver.exe");
} else {
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver_linux");
}
if (System.getProperty("app.env") != null) { // If coming from Jenkins/Maven goal..
// This is for logging results. Added when investigating crashes on chrome driver. Can be disabled when not needed. 26/03/2020
System.setProperty("webdriver.chrome.verboseLogging", "true");
}
unknown-error-devtoolsactiveport-file-doesnt-exist-while-t
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
options.addArguments("--window-size=1920x1080");
options.addArguments("--disable-cache");
//options.addArguments("--headless");
options.addArguments("--disable-application-cache");
options.addArguments("--disk-cache-size=0");
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--dns-prefetch-disable");
//options.addArguments("--no-sandbox"); // Bypass OS security model
options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
driver = new ChromeDriver(options);
//--------------------
return driver;
}
}
It is described here.
https://applitools.com/blog/automating-electron-applications-using-selenium/
You just need to set appropriate options and use same code for the chrome and electron.
#Before
public void setup() {
ChromeOptions opt = new ChromeOptions();
opt.setBinary("/Users/yanir/Downloads/Electron API Demos.app/Contents/MacOS/Electron API Demos");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("chromeOptions", opt);
capabilities.setBrowserName("chrome");
driver = new ChromeDriver(capabilities);
if (driver.findElements(By.id("button-about")).size() > 0)
driver.findElement(By.id("button-about")).click();
}

Selenium on Jenkins is skipping all the tests

Java: 8
Selenium: 3.14
Browser: Firefox 62.0.2
Geckodriver: v0.22.0
The Selenium execution of my project on Jenkins is skipping all tests:
Jenkins log
This is the dependencies of selenium on my build.gradle file:
['org.seleniumhq.selenium:selenium-java:3.14.0'],
['org.seleniumhq.selenium:selenium-server:3.14.0'],
['org.seleniumhq.selenium:selenium-api:3.14.0'],
['org.seleniumhq.selenium:selenium-support:3.14.0'],
['org.seleniumhq.selenium:selenium-remote-driver:3.14.0'],
['org.seleniumhq.selenium:selenium-firefox-driver:3.14.0'],
['org.seleniumhq.selenium:selenium-chrome-driver:3.14.0']
And here is where I set the geckodriver path. I'm using only firefox:
#Before
public void openResources() {
if( webDriver == null ){
String geckodriver = seleniumProperties.getString("selenium.caminhoGeckodriver");
try {
String browser = seleniumProperties.getString("selenium.browser");
if (!StringUtils.isEmpty(browser) && browser.toLowerCase().equals("chrome")) {
String path = seleniumProperties.getString("selenium.browser.path");
System.setProperty("webdriver.chrome.driver", path);
webDriver = new ChromeDriver();
} else {
System.setProperty("webdriver.gecko.driver", geckodriver);
webDriver = new FirefoxDriver();
}
} catch (MissingResourceException e) {
System.setProperty("webdriver.gecko.driver", geckodriver);
webDriver = new FirefoxDriver();
}
}
webDriver.manage().timeouts().implicitlyWait(TIMEOUT, TimeUnit.SECONDS);
webDriver.manage().timeouts().pageLoadTimeout(TIMEOUT, TimeUnit.SECONDS);
webDriver.manage().timeouts().setScriptTimeout(TIMEOUT, TimeUnit.SECONDS);
efetuarLogin();
}
The geckodriver path is set right on my seleniumProperties. The firefox version installed on the environment is 62.0.2.
On Eclipse, the tests are not skipped.
UPDATE:
That's the piece of code where is defined the task runSelenium (check the image with the Jenkins log that I posted):
task runSelenium(type: Test) {
include( '**/myProjectSuiteSelenium.class')
maxHeapSize = "1524m"
jvmArgs "-XX:MaxPermSize=512m", "-XX:-UseSplitVerifier"
}
test.finalizedBy runSelenium
include( '**/myProjectSuiteSelenium.class')
Remove this include and replace it with something that matches your actual tests.
I see "selenium.*" in the Jenkins logs.
I figured out what was the problem.
The problem was that the Jenkins wasn't logging the real error. My seleniumProperties was referencing the wrong file with the selenium properties. So it wasn't getting the right geckodriver file.
Once I change to the right file, the tests were not skipped anymore.

getting error with chromedriver using RemoteWebdriver

Getting error:
FAILED CONFIGURATION: #BeforeMethod setUp
org.openqa.selenium.WebDriverException: The path to the driver
executable must be set by the webdriver.chrome.driver system property;
for more information, see
http://code.google.com/p/selenium/wiki/ChromeDriver. The latest
version can be downloaded from
http://chromedriver.storage.googleapis.com/index.html
My code :
capability = DesiredCapabilities.chrome();
capability.setBrowserName("chrome");
capability.setVersion("38.0.2125.122 m");
String strChromePath = System.getProperty("user.dir")
+ "\\webdrivers\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", strChromePath);
capability.setPlatform(org.openqa.selenium.Platform.ANY);
return new RemoteWebDriver(new URL("http://192.168.1.77:5555/wd/hub"),
capability);
On the above code chromedriver it self is not getting invoked.
Then i tried with code:
ChromeDriverService chromeService = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("webdrivers/chromedriver.exe"))
.usingAnyFreePort().build();
chromeService.start();
capability = DesiredCapabilities.chrome();
capability.setBrowserName("chrome");
capability.setVersion("38.0.2125.122 m");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
return new RemoteWebDriver(new URL("http://192.168.1.77:5555/wd/hub"),
capability);
On executing above code the executable is launched but chrome is not invoked. It throws the same error. Code is working fine for firefox. Any help please?
Download the relevant Chrome driver as per your system(32-bit/64-bit), from here . Try setting the property of ChromeDriver first, like this:
File file = new File("D:\\chromedriver.exe"); //path to the chromedriver.exe so downloaded
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
Then use this code:-
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setBrowserName("chrome");
capability.setVersion("38.0.2125.122 m");
WebDriver driver = new RemoteWebDriver(new URL("http://192.168.1.77:5555/wd/hub"),capability);
If there is no need of using "RemoteWebDriver", you can code just use this below :
File file = new File("D:\\chromedriver.exe"); //path to the chromedriver.exe so downloaded
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
Try below :
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "properties/chromedriver.exe");
driver = new ChromeDriver();
driver.get("www.google.com");
Put chrome driver in properties folder.

Appium TouchAction/ MultiTouchAction don't work when testing Mobile Web

I'm trying to set up an automated test for a mobile website using Appium 1.2.4, Java-client 2.0.0, Selenium 2.42.2 and a real device (Samsung Galaxy Tab-3).
The problem is that, whenever I'm trying to use Touch Actions (e.g. driver.swipe() or driver.pinch()) I'm getting a Not Yet Implemented … error.
I've tried to improvise by switching the driver context to NATIVE_APP before executing the TouchAction and yet it still didn't work (I get a different error: An unknown server-side error occurred while processing the command).
Would appreciate your kind advice.
Configuration:
Selenium 2.42.2 Java-client 2.0.0 Appium 1.2.4
Device:Galaxy Tab3
OS: Android 4.4.2
Browser: Chrome 38
Thanks and Best Regards,
Daniel.
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability("device","ANDROID");
capability.setCapability("app", getValidBrowserName(browser));
capability.setCapability("newCommandTimeout",150);
capability.setCapability("platformName", "Android");
capability.setCapability("deviceName", "GalaxyTab3");
capability.setCapability("platformVersion", "4.4.2");
RemoteWebDriver driver = null;
Platform currentPlatform = desiredCapabilities.getPlatform();
boolean isMobilePlatform = currentPlatform.equals(Platform.ANDROID) || currentPlatform.equals(Platform.MAC);
URL hubURL = new URL(hubHost + "/wd/hub");
try {
driver = isMobilePlatform ? new AndroidDriver(hubURL, desiredCapabilities) : new RemoteWebDriver(hubURL, desiredCapabilities);
driver.manage().timeouts().implicitlyWait(waitTimeout, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(scriptTimeout, TimeUnit.SECONDS);
driver.setFileDetector(new LocalFileDetector());
}
catch (Exception e) {
throw new Exception("Failed to create Selenium WebDriver instance on hub: "
+ hubURL.toString() + " with capabilities: "
+ desiredCapabilities.toString() + "\n"
+ e.getMessage());
}
WebElement buttonTest = driver.findElement(By.id("configBtn"));
int buttonX = buttonTest.getLocation().getX();
int buttonY = buttonTest.getLocation().getY();
((AppiumDriver)driver).swipe(buttonX, buttonY, buttonX + 70, buttonY + 70, 1000);

Selenium Chromedriver causes Chrome to start without configured plugins, bookmarks and other settings

I am a new user of Selenium. I want to use it to start up the Chrome browser but I have a problem.
public static void processor(String url, String name) {
System.setProperty("webdriver.chrome.driver", "C:/Documents and Settings/jingxiong/Local Settings/Application Data/Google/Chrome/Application/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(url);
WebElement element = driver.findElement(By.name(name));
element.sendKeys("google");
element.submit();
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
When I run this example the Chrome browser starts ok but without configured plugins, my settings or bookmarks. What should I do to cause it load these?
Thank you.
You should first read chromedriver document in selenium wiki. Its available here - http://code.google.com/p/selenium/wiki/ChromeDriver
As mentioned in the wiki:-
Similarly, to load an extension when Chrome starts:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--load-extension=/path/to/extension/directory"));
WebDriver driver = new ChromeDriver(capabilities);