Trying to run an eclipse JAVA program in Sauce Labs. But getting an issue with constructor and remote webdriver. - selenium

Written below code to run an eclipse program in Sauce Labs. Workflow is showing an issue "The constructor Remote WebDriver(String, DesiredCapabilities) is undefined".
Still I tried to run the program then below issue came.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor RemoteWebDriver(URL, DesiredCapabilities) is undefined
The constructor URL(String) is undefined at sauceprog.main(sauceprog.java:40)
CODE:
import java.net.MalformedURLException;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import com.gargoylesoftware.htmlunit.javascript.host.URL;
public class sauceprog
{
public static void main(String[] args) throws InterruptedException, MalformedURLException
{
String browser = "safari-saucelabs";
String URL = "https://rpautomation4:27763475-7193-4984-8074-a4fe2f9982f7#ondemand.saucelabs.com:443/wd/hub";
DesiredCapabilities caps = null;
switch(browser.toLowerCase())
{
case "internetexplorer-win-saucelabs":
caps = DesiredCapabilities.internetExplorer();
caps.setCapability("platform", "Windows 10");
caps.setCapability("version", "11.103");
caps.setCapability("recordVideo", "false");
caps.setCapability("screenResolution", "1024x768");
break;
case "chrome-win-saucelabs":
caps = DesiredCapabilities.chrome();
caps.setCapability("platform", "Windows 8");
caps.setCapability("version", "69.0");
caps.setCapability("recordVideo", "false");
break;
case "firefox-win-saucelabs":
caps = DesiredCapabilities.firefox();
caps.setCapability("platform", "Windows 8");
caps.setCapability("version", "62.0");
caps.setCapability("recordVideo", "false");
default:
System.out.println("you passed incorrect values. Please check next time");
}
RemoteWebDriver driver = new RemoteWebDriver(URL,caps);
driver.get("https://www.google.com");
driver.findElement(By.name("q")).sendKeys("selenium");
Thread.sleep(2000);
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
driver.quit();
}
}

You are using an incorrect import statement. Ideally, your program should use import java.net.URL; instead of import com.gargoylesoftware.htmlunit.javascript.host.URL;
I have posted the complete updated code snippet below:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
public class sauceprog
{
public static void main(String[] args) throws InterruptedException, MalformedURLException
{
String browser = "safari-saucelabs";
String URL = "https://<sauce_user>:<key>#ondemand.saucelabs.com:443/wd/hub";
DesiredCapabilities caps = null;
switch(browser.toLowerCase())
{
case "internetexplorer-win-saucelabs":
caps = DesiredCapabilities.internetExplorer();
caps.setCapability("platform", "Windows 10");
caps.setCapability("version", "11.103");
caps.setCapability("recordVideo", "false");
caps.setCapability("screenResolution", "1024x768");
break;
case "chrome-win-saucelabs":
caps = DesiredCapabilities.chrome();
caps.setCapability("platform", "Windows 8");
caps.setCapability("version", "69.0");
caps.setCapability("recordVideo", "false");
break;
case "firefox-win-saucelabs":
caps = DesiredCapabilities.firefox();
caps.setCapability("platform", "Windows 8");
caps.setCapability("version", "62.0");
caps.setCapability("recordVideo", "false");
default:
System.out.println("you passed incorrect values. Please check next time");
}
RemoteWebDriver driver = new RemoteWebDriver(new URL(URL),caps);
driver.get("https://www.google.com");
driver.findElement(By.name("q")).sendKeys("selenium");
Thread.sleep(2000);
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
driver.quit();
}
}
Also, I would suggest you update your sauce username and key if not already done. Since you posted the username and key publicly, anyone can run their tests on your account using the same.

Related

i need change lenguage of microsoft edge for selenium automation --lang=es

i need change the language to --lang=es in microsoft-edge selenium java.
Because can't the page see in english and need in spanish
Try this code, its working:
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from selenium.webdriver.edge.options import Options
options = Options()
options.add_argument("--lang=es")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
prefs = {"translate_whitelists": {'en':'es'},
"translate":{"enabled":"true"}}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()), options=options)
driver.maximize_window()
driver.get("https://www.microsoft.com")
Based on AbiSaran's answer (which is in Python), I modified it into a Java one.
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
public class EdgeTest {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.edge.driver", "C:\\path\\to\\msedgedriver.exe");
EdgeOptions options = new EdgeOptions();
options.addArguments("--lang=es");
EdgeDriver driver = new EdgeDriver(options);
try {
driver.get("https://bing.com");
WebElement element = driver.findElement(By.id("sb_form_q"));
element.sendKeys("WebDriver");
element.submit();
Thread.sleep(5000);
} finally {
driver.quit();
}
}
}

Junit 5 tests are not running in parallel

I have created a simple program to intialte crome and one SYSO statement. I want to run the 2 #Test in parallel. But it is executing alwasy is seq. What could be teh reason for it to not recognise the parallel execution?
Here is the code:
import static org.junit.jupiter.api.Assertions.*;
import java.util.HashMap;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Proxy.ProxyType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
#Execution(ExecutionMode.CONCURRENT)
public class Parallel {
static String driverPath = "C:/BestX/workspace/chromedriver_win32/chromedriver.exe";
static String downloadFilepath = "C:\\BestX";
#Execution(ExecutionMode.CONCURRENT)
#Test
void test1() throws Exception {
createDriver();
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName()+" => test1"); //.currentThread.getName() -just to display current running thread name
}
#Execution(ExecutionMode.CONCURRENT)
#Test
void test2() throws Exception {
createDriver();
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName()+" => test1"); //.currentThread.getName() -just to display current running thread name
}
public void createDriver() {
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability("takesScreenshot", true);
System.setProperty("webdriver.chrome.driver", driverPath);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments(new String[] { "--no-sandbox" });
chromeOptions.addArguments(new String[] { "--max_old_space_size=4096" });
dcaps.setCapability("goog:chromeOptions", chromeOptions);
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", downloadFilepath);
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(chromeOptions);
driver.manage().window().setSize(new Dimension(1920, 1200));
}
}
'''
and here is the "junit-platform.properties" under resource folder
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
It is workign now. Was a silly mistake in folder structure. The junit-platform.properties file was under tests->resources instead of test->resources folder

Groovy, Selenium, Cucumber, adding ChromeOptions arguments

I wanted to create a BaseTest.groovy where i implement the Webdriver with headless mode.
package api
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
class BaseTest{
ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArguments(["--headless", "--no-sandbox"])
static WebDriver driver = new ChromeDriver(chromeOptions)
}
I have a LoginSteps.groovy stepdefinitions file
package stepDefinitions
import api.Helper.helper
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import static cucumber.api.groovy.EN.*
Given(~/^I am on the xyz login page$/) { ->
helper.setPage("https://xyzTestpage.com/")
}
When(~/^I sign in as "([^"]*)"$/) { String arg1 ->
helper.signIn("username","password")
}
Then(~/^I load the homepage$/) { ->
helper.setPreset()
}
And i have a helper.groovy file where i implement the methods
package api.Helper
import api.BaseTest
import api.Xpaths.LoginPageXpaths
import api.Tools.tools
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
class helper extends BaseTest {
static void setPage(String url){
driver.get(url)
}
static void signIn(String username, String password){
WebElement uname = driver.findElement(By.xpath(LoginPageXpaths.userNameField()))
uname.sendKeys(username)
WebElement pwd = driver.findElement(By.xpath(LoginPageXpaths.passWordField()))
pwd.sendKeys(password)
WebElement loginButton = driver.findElement(By.xpath(LoginPageXpaths.loginButton()))
loginButton.click()
}
static void setPreset(){
WebElement multiCountry = driver.findElement(By.xpath(LoginPageXpaths.multiCountryButton()))
multiCountry.click()
WebElement openButton = driver.findElement(By.xpath(LoginPageXpaths.openButton()))
openButton.click()
String inputWindow = driver.getWindowHandle()
for(String loggedInWindow : driver.getWindowHandles()){
driver.switchTo().window(loggedInWindow)
}
WebElement lineItem = driver.findElement(By.xpath(LoginPageXpaths.calculateButtonXpath()))
tools.waitForElementToBeClickable(driver,lineItem,25)
driver.quit()
}
}
So my problem is, i don't know where should i set the headless mode, because i got error, when i run this.
Can you please try adding arguments separately as below and run it
class BaseTest{
ChromeOptions chromeOptions = new ChromeOptions()
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--no-sandbox");
static WebDriver driver = new ChromeDriver(chromeOptions)
}

Why the driver.getTitle does not matches with the expected result in Selenium?

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class LoginAndSearch {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\Users\\puren\\Downloads\\Compressed\\geckodriver-v0.11.1-win64\\geckodriver.exe");
DesiredCapabilities capabilities=DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.linkedin.com");
String a = driver.getTitle();
if (a=="LinkedIn: Log In or Sign Up")
System.out.print("Pass");
else
System.out.println("Fail");
driver.close();
}
}
In this code, the "If condition" does not match with the expected result.
You are comparing for references of String objects. To compare string values change your code snippet as follows:
String a = driver.getTitle();
if (a.equals("LinkedIn: Log In or Sign Up"))
System.out.print("Pass");
else
System.out.println("Fail");
Follow the link for more:
What is the difference between == vs equals() in Java?

Setup proxy in Firefox browser via marionette driver

Firefox 47 and above do not support Selenium Webdriver. I tried to use a Marionette driver to start my tests via Firefox.
But my settings in firefox-profile (proxy must set to network.proxy.type = 4, auto-detect) is no longer applied to Firefox config (Firefox opens but all settings set by default) and my tests do not work without the right PROXY configuration.
How can I setup proxy in Firefox browser via a Marionette driver?
The old tricks with firefoxProfile or Proxy class do not work any more. All you have to do is pass through requiredCapabilities a JSON with the new Marionette proxy format:
[{proxy={"proxyType":"MANUAL","httpProxy":"host.proxy","httpProxyPort":80,"sslProxy":"host.proxy","sslProxyPort":80}}]
No more "proxyHost:proxyPort" format, but httpProxy=host, httpProxyPort=port.
JsonObject json = new JsonObject();
json.addProperty("proxyType", "MANUAL");
json.addProperty("httpProxy", aHost);
json.addProperty("httpProxyPort", aPort);
json.addProperty("sslProxy", aHost);
json.addProperty("sslProxyPort", aPort);
required.setCapability("proxy", json);
driver = new FirefoxDriver(service, cap, required);
Here is all the code:
import java.io.File;
import java.io.IOException;
import org.apache.commons.lang.SystemUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;
public class TestProxy
{
public static void main(String[] args)
{
if (args.length == 0)
{
System.out.println("usage: IP port");
System.exit(-1);
}
String proxyServer = args[0];
int proxyPort = Integer.parseInt(args[1]);
try
{
TestProxy test = new TestProxy();
test.TestDriver(proxyServer, proxyPort);
} catch (IOException e)
{
e.printStackTrace();
}
}
private void TestDriver(String aHost, int aPort) throws IOException
{
WebDriver driver = null;
GeckoDriverService service = null;
if (SystemUtils.IS_OS_LINUX)
{
FirefoxBinary firefoxBinary = new FirefoxBinary(new File("/home/ubuntu/firefox/firefox"));
service = new GeckoDriverService.Builder(firefoxBinary)
.usingDriverExecutable(new File("/usr/bin/geckodriver"))
.usingAnyFreePort()
.usingAnyFreePort()
.withEnvironment(ImmutableMap.of("DISPLAY",":20"))
.build();
service.start();
}
else if (SystemUtils.IS_OS_WINDOWS)
{
FirefoxBinary firefoxBinary = new FirefoxBinary(new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe"));
service = new GeckoDriverService.Builder(firefoxBinary)
.usingDriverExecutable(new File("C:\\Windows\\geckodriver.exe"))
.usingAnyFreePort()
.usingAnyFreePort()
.build();
service.start();
}
else
{
System.out.println("Unknown operation system");
System.exit(-1);
}
DesiredCapabilities cap = DesiredCapabilities.firefox();
DesiredCapabilities required = new DesiredCapabilities();
JsonObject json = new JsonObject();
json.addProperty("proxyType", "MANUAL");
json.addProperty("httpProxy", aHost);
json.addProperty("httpProxyPort", aPort);
json.addProperty("sslProxy", aHost);
json.addProperty("sslProxyPort", aPort);
required.setCapability("proxy", json);
driver = new FirefoxDriver(service, cap, required);
driver.navigate().to("https://api.ipify.org/?format=text");
System.out.println(driver.getPageSource());
driver.quit();
}
}
https://github.com/SeleniumHQ/selenium/issues/2963
https://github.com/mozilla/geckodriver/issues/97#issuecomment-255386464