How to Configure the Ports for Communication between Selenium Server and the Browsers - selenium

I'm using Selenium Standalone Server 3.141.59 https://www.seleniumhq.org/download
In my code, when a WebDriver is created the Selenium server debugs something like: Starting ChromeDriver on port 28208
Is it possible to configure a range of ports (e.g., 28000-28100) that are allowed to be used by the Selenium server?

Use below code to configure chrome to run on other then default port.
int desiredPortNo = 22300;
ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(new File("chrome_driver_path")).usingPort(desiredPortNo).build();
WebDriver driver = new ChromeDriver(service);
Update
To use with RemoteWebDriver :
int desiredPortNo = 22300;
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("...", true);
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("driver_path")).usingPort(desiredPortNo)
.build();
service.start();
WebDriver driver = new RemoteWebDriver(service.getUrl(),capabilities);
driver.get("site_url");

Related

How to setup proxy in QAF

I need to setup proxy in my QAF test method. Below code is working fine without QAF. How to setup the same with QAF implementation?
// #Test
public void sampleTest() {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--ignore-certificate-errors");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
#SuppressWarnings("deprecation")
ChromeDriver driver = new ChromeDriver(capabilities); // Here opening new window and able
to hit my localhost:8080
//i need to use following QAF methods which also needs to trigger my localhost
get("http://demo.rapidtestpro.com/login.php");
sendKeys("1234567", "//*[#id='accno']");
sendKeys("password123", "//*[#id=\"pass\"]");
System.out.println("Exit in sample test");
}
Also I tried to setup proxy in application.properties file like below
system.http.proxyHost=localhost
system.http.proxyPort=8080
Also tried in Testngconfig.xml file like below
<test name="java Test" enabled="true">
<parameter name="driver.name" value="chromeDriver"/>
<parameter name="system.http.proxyHost" value="localhost"/>
<parameter name="system.http.proxyPort" value="8080"/>
Nothing is triggered in localhost:8080.
As per your working code, you want to set proxy for driver using capabilities. When you are using qaf you can provide driver capabilities in different ways. Simplest way is by setting appropriate property with json value of desired capability. In your case it will look like as below:
driver.name=chromeDriver
chrome.additional.capabilities={"goog:chromeOptions":{"args":["start-maximized","--ignore-certificate-errors"]},"proxy":{"httpProxy":"localhost:8080","sslProxy":"localhost:8080"}}
If you want to set proxy capability for all browsers :
driver.name=<driver name>
#additional capabilities for any driver
driver.additional.capabilities={"proxy":{"httpProxy":"localhost:8080","sslProxy":"localhost:8080"}}
#additional capabilities only for chrome
chrome.additional.capabilities={"goog:chromeOptions":{"args":["start-maximized","--ignore-certificate-errors"]}}
If you don't know what will be the json representation for capability you can print in console and take a reference. For example blow code with print json value of capabilities that you refereed in question:
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("proxy", proxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("--ignore-certificate-errors");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.out.println(JSONUtil.toString(capabilities.toJson()));
}
Other way is using driver listener, where you can append capabilities in void beforeInitialize(Capabilities capabilities) For example:
void beforeInitialize(Capabilities capabilities){
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:8080");
proxy.setSslProxy("localhost:8080");
DesiredCapabilities capabilities = (DesiredCapabilities)capabilities;
capabilities.setCapability("proxy", proxy);
}

Is there an Internet Explorer webdriver configuration for ignoring security certificates on serenity?

For context, I use a serenity.properties file for my webdriver configurations.
I am using serenity 2/cucumber 4/java.
I wanted to use something similar to how Chrome driver works with serenity properties.. Something like:
chrome.capabilities.acceptInsecureCerts = true
Which allows me to bypass that the security certificate error for Chrome Driver.
But I couldn't find something similar to this configuration for the serenity IE driver.
My question is: is there a way to do this via serenity.properties configurations similarly for IE driver, rather then having to declare and pass something like this?
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.ie.driver","IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(capabilities);
source
Things I've tried while running on my selenium grid:
iexplorer.capabilities.acceptInsecureCerts = true
iexplorer.capabilities.acceptSslCerts = true
iexplorer.capabilities.introduceFlakinessByIgnoringSecurityDomains = true
iexplorer.capabilities.setJavascriptEnabled = true
Should iexplorer.capabilities even work?
Example of security certificate error on IE:
I have checked the selenium IE webdriver document and github forum, and search lots of resources about how to handle the SSL Certificate in Selenium IE WebDriver. It seems that we could only set the ACCEPT_SSL_CERTS property via the DesiredCapabilities method, and use the following code for IE Webdriver:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.ie.driver","IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(capabilities);

Selenium test on VM with Geckodriver

Hi I have been trying to run a test on a virtual machine. I have gone through the Internet and none of the solutions worked for me.
I want to open firefox on Windows virtual machine. Here is my code:
#BeforeTest
public void launchapp() throws MalformedURLException
{
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
String URL = "http://www.google.com";
System.out.println(" Executing on FireFox");
String Node = "http://XX.XX.X.XX:5555/wd/hub";
URL url = new URL(Node);
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability("marionette", true);
desiredCapabilities.setBrowserName("firefox");
//driver = new FirefoxDriver(cap);
driver = new RemoteWebDriver(url, desiredCapabilities);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.navigate().to(URL);
driver.manage().window().maximize();
}
The error I get on the virstual machine is:
The path to the driver executable must be set by the webdriver gecko.driver system property....
On my PC I only see on the console that its trying to connect the node but it fails:
Marking the node http://.... as down: cannot reach the node for 2 tries
and
Unregistering the node http://... because it's been down for XXX ms
and in Eclipse:
org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{marionette=true, browserName=firefox, version=, platform=ANY}], required capabilities = null
Do you have any idea why this is not working? What should I check?

Browser Plugin Testing With Selenium

I am writing a webapp that has a browser plugin component for both firefox and chrome. My current testing system uses a series of Selenium tests created through Selenium IDE.
Is it possible to also have selenium install, activate, and delete browser plugins for firefox and chrome (possibly other browsers as well)?
I think the biggest concern is that installing/enabling the browser plugin requires a browser restart, and I'm not sure if that would through selenium off.
The acquisition of the plugin is easily handled by visiting an internal site-link to a php-script that detects your browser.
The answer is Yes, Selenium 2 supports (remote) installation of browser extensions.
The Chrome and Firefox WebDriver support the installation of extensions, remotely. Here's sample code for Chrome and Firefox:
Chrome
File file = new File("extension.crx"); // zip files are also accepted
ChromeOptions options = new ChromeOptions();
options.addExtensions(file);
// Option 1: Locally.
WebDriver driver = new ChromeDriver(options);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Firefox
File file = new File("extension.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
// Option 1: Locally
WebDriver driver = new FirefoxDriver(firefoxProfile);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
I have also implemented automated installation of Opera and Safari extensions, and they have been merged upstream:
OperaDriver: https://github.com/operasoftware/operadriver/pull/93
SafariDriver: https://github.com/SeleniumHQ/selenium/pull/87
Opera
This API is similar to the FirefoxDriver.
File file = new File("extension.oex"); // Must end with ".oex"
OperaProfile operaProfile = new OperaProfile();
operaProfile.addExtension(file);
// Option 1: Locally
WebDriver driver = new OperaDriver(operaProfile);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.opera();
capabilities.setCapability("opera.profile", operaProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Safari
This API is similar to the ChromeDriver.
File file = new File("extension.safariextz");
SafariOptions options = new SafariOptions();
options.addExtensions(file);
// Option 1: Locally.
WebDriver driver = new SafariDriver(options);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.safari();
capabilities.setCapability(SafariOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Internet Explorer
Good luck.
Short answer: no
Installing a browser extension is outside of the scope of handling in Selenium.
In Chrome, it displays a modal window that is not "clickable" with Selenium when you want to add a plugin or app. Chrome does not require restarting.
Firefox has the same kind of behaviour to prompt for extension permissions.
You can try something that resides outside of the browser to do what you want. Sikuli might do the trick.

Using Selenium to test Safari gives GridException

I am using the code from this site (http://darrellgrainger.blogspot.com/2011/02/using-selenium-20-with-webdriver-and.html) to run Selenium tests in Safari 5. The code goes like this:
Selenium sel = new DefaultSelenium("localhost", 4444, "*safari", baseURL);
CommandExecutor executor = new SeleneseCommandExecutor(sel);
DesiredCapabilities dc = new DesiredCapabilities();
WebDriver browser = new RemoteWebDriver(executor, dc);
browser.get("http://www.google.com");
WebElement input = browser.findElement(By.name("q"));
input.sendKeys("Selenium");
So I start a Selenium server standalone version on the localhost machine and I register a test node (also on localhost) to the Selenium hub. Then I start the test. I then get the following exception: org.openqa.selenium.WebDriverException: Could not start Selenium session: org%2Eopenqa%2Egrid%2Ecommon%2Eexception%2EGridException%3A+Error+forwarding+the+new+session+The+server+returned+an+error+%3A+
I don't know what error. There is no server output on the console. Does anyone have ideas? I used the newest version (2.17.0) of Selenium.
EDIT: I just tried "firefox" instead of safari and it outputs the same exception. So actually it's not the fault of safari. Maybe there is something wrong with executing Selenium 1 code via the grid?
Try This:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("safari");
capabilities.setJavascriptEnabled(true);
CommandExecutor executor = new SeleneseCommandExecutor(new URL("http://localhost:5555/"), new URL("http://www.google.com/"), capabilities);
WebDriver driver = new RemoteWebDriver(executor, capabilities);
driver.get("http://google.com");
Don't create DefaultSelenium object. The above code works well for me with Safari browser.