I am trying to test some proxies on www.google.com but Selenium gets stopped at not working proxies without scanning remaining proxies:
package hello1;
import java.time.Duration;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Hello1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
// TODO Auto-generated method stub
String[] proxy = {"65.153.193.135:80","145.40.73.102:10004","203.189.142.168:53281","42.62.179.243:8080","200.24.146.68:999"};
for(int i=0;i<proxy.length;i++)
{
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless","--disable-gpu","--ignore-certificate-errors");
options.addArguments("--proxy-server=https://" + proxy[i]);
options.setAcceptInsecureCerts(true);
WebDriver webDriver = new ChromeDriver(options);
webDriver.get("https://www.google.com");
System.out.println("Page title is : " + webDriver.getTitle()+ proxy[i]);
webDriver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(5));
webDriver.quit();
}
}
}
Errors:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: net::ERR_PROXY_CONNECTION_FAILED
(Session info: headless chrome=99.0.4844.51)
Capabilities {acceptInsecureCerts: true, browserName: chrome, browserVersion: 99.0.4844.51, chrome: {chromedriverVersion: 99.0.4844.51 (d537ec02474b5..., userDataDir: C:\Users\hp\AppData
Related
I have run my test script but I got this error message in the console tab:
"Starting ChromeDriver 84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147#{#310}) on port 30846
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Aug 22, 2020 5:41:42 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C"
Below is my java code:
package seleniumProjectTutorial;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumWebDriver {
WebDriver driver;
public void invokeBrowser() {
try {
System.setProperty("webdriver.chrome.driver","D:\\Selenium\\Drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("https://www.google.com");
driver.close();
driver.quit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
SeleniumWebDriver myobject = new SeleniumWebDriver();
myobject.invokeBrowser();
}
}
Is there a need for me to fix it and how? Thank you.
If you wanted to stop this warning message in the console you need to pass --silent argument to chromedriver to stop console message. you can achieve this using 'withSilent(true)' method
Launch chromedriver using chromedriverservice as shown in below sample code
ChromeDriverService cdservice=new ChromeDriverService.Builder().usingDriverExecutable(new File("/path/to/chromedriver.exe"))
.withLogFile(new File("/path/to/chromedriver.log"))
.withSilent(true)
.usingAnyFreePort()
.build();
WebDriver driver = new ChromeDriver(cdservice);
driver.get("http://www.google.com");
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)
}
I am new to jenkins. I have installed jenkins and plugins. And port 8080 I am using it. While running my test case in Jenkins I am getting below error "java.lang.AssertionError: Unsupported type parameter".
I am using TestNG . But the same is running fine when I am running in Eclipse by run as TestNG. Please help me to resolve this.
java.lang.AssertionError: Unsupported type parameter : class com.volvo.tests.properties.ChosenWebDriver
[TestNG] at org.testng.internal.Parameters.convertType(Parameters.java:246)
[TestNG] at org.testng.internal.Parameters.createParameters(Parameters.java:165)
[TestNG] at org.testng.internal.Parameters.createParameters(Parameters.java:355)
[TestNG] at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:86)
[TestNG] at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:199)
[TestNG] at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
[TestNG] at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
[TestNG] at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
[TestNG] at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
[TestNG] at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
[TestNG] at org.testng.TestRunner.privateRun(TestRunner.java:767)
[TestNG] at org.testng.TestRunner.run(TestRunner.java:617)
-------------------------------------------
"ChosenWebDriver"
package com.volvo.tests.properties;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public enum ChosenWebDriver {
IE("IE"), CHROME("CHROME"), Firefox("FF");
private final String chosenDriver;
private ChosenWebDriver(String driver) {
chosenDriver = driver;
}
public WebDriver getDriver() {
WebDriver ret = null;
switch (chosenDriver) {
case "IE":
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability("nativeEvents", false);
capabilities.setCapability("requireWindowFocus", true);
// capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL,
// "http://www.bing.com/");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "IE");
// capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION,
// true);
ret = new InternetExplorerDriver(capabilities);
break;
case "CHROME":
try {
if (Platform.getCurrent() != null && Platform.getCurrent().is(Platform.LINUX)) {
ChromeDriverService chromeDriverService = new ChromeDriverService.Builder().usingDriverExecutable(new File(
"/usr/bin/chromedriver")).usingAnyFreePort().withLogFile(new File("/home/jenkins/.config/google-chrome/chromedriver.log")).build();
chromeDriverService.start();
ret = new ChromeDriver(chromeDriverService, getChromeCapabilities());
} else {
/*
* ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
* .usingDriverExecutable(new File("selenium_drivers/chromedriver.exe")).usingAnyFreePort()
* .withLogFile(new File("selenium_drivers/chromedriver.log")).build();
* chromeDriverService.start();
*/
ret = new ChromeDriver();
}
} catch (IOException e) {
e.printStackTrace();
}
break;
case "FF":
ret = new FirefoxDriver();
break;
}
return ret;
}
// Setting Chrome desired capabilities:
private DesiredCapabilities getChromeCapabilities() {
// System.setProperty("webdriver.chrome.driver",
// "/usr/bin/chromedriver");
// System.setProperty("webdriver.chrome.logfile",
// "/home/jenkins/.config/google-chrome/chromedriver.log");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// capabilities.setCapability(CapabilityType.PLATFORM, "Linux
// 3.10.0-327.el7.x86_64 x86_64");
capabilities.setCapability(CapabilityType.PLATFORM, Platform.getCurrent());
capabilities.setCapability(ChromeOptions.CAPABILITY, getChromeOptions());
capabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
capabilities.setBrowserName("chrome");
return capabilities;
}
// Setting Chrome options:
private ChromeOptions getChromeOptions() {
ChromeOptions options = new ChromeOptions();
// options.addArguments("user-data-dir=".concat("/home/jenkins/.config/google-chrome"));
options.addArguments("profile-directory=".concat("Default"));
options.addArguments("disable-extensions");
options.addArguments("disable-plugins");
options.addArguments("headless");
options.addArguments("window-size=1920x1080");
options.addArguments("no-sandbox");
options.addArguments("disable-gpu");
options.addArguments("enable-logging --v=4");
// options.setBinary(new File("/usr/bin/google-chrome-beta"));
return options;
}
}
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.
Need to register multiple nodes to the hub, Any help in this regards will be appreciated.Selenium grid to be performed with the same driver object.
This is how my code looks like.
Deriving the class using RemoteWebDriver for Hub instance.
import java.net.URL;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import com.project.pages.ExcelConstants;
public class DriverInitializer {
private WebDriver driver = null;
public WebDriver getWebDriverFactory() {
return this.driver;
}
/**
* Returns WebDriver Instance Depending on Parameters browser and Grid d
*
* #param browser
* #param grid
* #return
*/
public WebDriver getDriver(String browser, String grid) {
if (grid.equals("y")) {
getWebDriverRemote(browser);
} else if (grid.equals("n")) {
getWebDriverLocal(browser);
}
return driver;
}
/**
* Creates Local WebDriver Instance
*
* #param browser
* #return
*/
public WebDriver getWebDriverLocal(String browser) {
try {
if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir")
+ "\\Drivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches",
Arrays.asList("ignore-certificate-errors"));
driver = new ChromeDriver(options);
return driver;
}
if (browser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver",
System.getProperty("user.dir")
+ "\\drivers\\IEDriverServer.exe");
DesiredCapabilities cap = DesiredCapabilities
.internetExplorer();
cap.setCapability(
InternetExplorerDriver.UNEXPECTED_ALERT_BEHAVIOR,
"accept");
cap.setCapability("nativeEvents", true);
cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL,
ExcelConstants.TEST_URL_2);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
return driver = new InternetExplorerDriver(cap);
}
if (browser.equals("firefox")) {
System.setProperty("webdriver.gecko.driver",
System.getProperty("user.dir")
+ "\\Drivers\\geckodriver.exe");
// Extra code added
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("acceptInsecureCerts", true);
// Extra code ended
return driver = new FirefoxDriver(capabilities);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return driver;
}
/**
* Creates Remote WebDriver Instance
*
* #param browser
* #return
*/
public WebDriver getWebDriverRemote(String browser) {
DesiredCapabilities cap = null;
try {
if (browser.equals("ie")) {
cap = DesiredCapabilities.internetExplorer();
cap.setCapability(
InternetExplorerDriver.UNEXPECTED_ALERT_BEHAVIOR,
"accept");
cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL,
ExcelConstants.TEST_URL_2);
cap.setCapability("nativeEvents", true);
cap.setBrowserName("internet explorer");
cap.setPlatform(Platform.ANY);
}
if (browser.equals("firefox")) {
cap = DesiredCapabilities.firefox();
cap.setCapability(
InternetExplorerDriver.UNEXPECTED_ALERT_BEHAVIOR,
"accept");
cap.setBrowserName("firefox");
cap.setPlatform(Platform.ANY);
}
if (browser.equals("chrome")) {
cap = DesiredCapabilities.chrome();
cap.setBrowserName("chrome");
cap.setPlatform(Platform.WINDOWS);
}
driver = new RemoteWebDriver(new URL(ExcelConstants.NODE_URL_1), cap);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return driver;
}
public static void main(String[] args) {
DriverInitializer di = new DriverInitializer();
di.getWebDriverRemote("chrome");
}
/**
* Implicit Wait
*/
public void waitForPageToLoad(WebDriver driver) {
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
/**
* Maximizes the window
*/
public void maximizeWindow(WebDriver driver) {
driver.manage().window().maximize();
}
}
I tried using the thread class but unable to implement it using the concept.
download selenium server stand alone jar from here
after that follow below step
1- start hub using below command
java -jar "your selenium serverstand-alone.jar file path here" -role hub
2- now register your first node with hub-
java -jar "your selenium-server-stand-alone.jar file path here" -role node -hub http://localhost:4444/grid/register -port 5556 -browser "browserName=chrome, maxInstances=5"
3- now start 3rd, 4th etc. by just changing -port and browser value
java -jar "your selenium-server-stand-alone.jar file path here" -role node -hub http://localhost:4444/grid/register -port 5558 -browser "browserName=firefox, maxInstances=5"
to execute on node using selenium server use RemoteWebDriver instead of firefox or chrome driver
for more details description follow below tutorial
https://www.guru99.com/introduction-to-selenium-grid.html