Selenium Webdriver remote setup - java

I have the selenium-server-standalone.jar running on my local machine, and the tests I want to run compiled on my remote machine, but I have no idea how I make the tests connect to the machine that will run the browser. Any help appreciated.
Update:
On my local machine (the one I will be running the browser on) I ran
java -jar selenium-server-standalone-2.25.0.jar -mode hub
on my remote machine (that I will run the tests from) I ran
java -jar selenium-server-standalone-2.25.0.jar -role webDriver -hub http://**My ip*:4444
my code contains the following:
#Before
public void setUp() throws Exception {
DesiredCapabilities capability = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://**My ip**:4444/wd/hub"),
capability);
baseUrl = "http://phy05:8080";
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(1920, 1080));
I am using Linux and my tests are written in Java

well. That's not a problem. I'd like to share how i resolved this issue.
I got VM (virtual machine) with jdk installed and selenium server running on VM. VM has IP:
192.168.4.52
I connected to it through(RDC-remote desktop connection). Installed needed browser on it(firefox 15). Open browser. Disabled all the updates and other pop ups.
I've got selenium tests pack on my local machine. And I run them on my VM.
Selenium setup is following:
import com.google.common.base.Function;
import com.thoughtworks.selenium.SeleneseTestBase;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
public class BaseSeleniumTest extends SeleneseTestBase {
static WebDriver driver;
#Value("login.base.url")
private String loginBaseUrl;
#BeforeClass
public static void firefoxSetUp() throws MalformedURLException {
// DesiredCapabilities capability = DesiredCapabilities.firefox();
DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
driver = new RemoteWebDriver(new URL("http://192.168.4.52:4444/wd/hub"), capability);
// driver = new FirefoxDriver(); //for local check
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(1920, 1080));
}
#Before
public void openFiretox() throws IOException {
driver.get(propertyKeysLoader("login.base.url"));
}
#AfterClass
public static void closeFirefox(){
driver.quit();
}
.....
this piece of code will run all the selenium tests on remote machine.
in the string driver = new RemoteWebDriver(new URL("http://192.168.4.52:4444/wd/hub"), capability);
you simply should mention IP of your machine and this should work.
Hope this helps you.

Related

Selenium chromedriver does not close properly

I'm using chromedriver and Java to run automatic web tests. To be able to use extensions in Chrome I'm using my existing browser profile. Since then I'm experiencing following:
run a selenium test with green result
open Chrome manualy (or run some test)
get error message "chrome application did not close properly"
I'm closing browser and the driver with this:
#AfterClass public static void tearDownClass() {driver.quit();}
I've tried:
#AfterClass public static void tearDownClass() {driver.close();}
but this closes just browser, not the driver.
After some attempts to fix I simulate pressing CTRL+SHIFT+Q:
package SSO_CWP_APPROVAL;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebDriver;
public class Keyboard_events {
public static WebDriver driver;
public Keyboard_events(WebDriver driver) {Keyboard_events.driver = driver;}
public void ctrl_shift_q() throws AWTException, InterruptedException {
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_CONTROL);
rob.keyPress(KeyEvent.VK_SHIFT);
rob.keyPress(KeyEvent.VK_Q);
rob.keyRelease(KeyEvent.VK_CONTROL);
rob.keyRelease(KeyEvent.VK_SHIFT);
rob.keyRelease(KeyEvent.VK_Q);
Thread.sleep(1000);
}
}
The sleep is in this case neccessary. Sleeping less time and get the error again.
#AfterClass public static void tearDownClass() throws AWTException, InterruptedException {K_events.ctrl_shift_q();driver.quit();}
binaries:
Version: Oxygen.3a Release (4.7.3a)
Build id: 20180405-1200
chromedriver.exe 2.42
JDK 8u151
Chrome 69.0.3497.100
Is there any better way how to close browser and the driver?
It was propably bug in Google Chrome 69. In version 70 and 71 works driver.close();driver.quit(); just fine.

Java Selenium "findElement" doesnt find an existing object

After my problem was solved yesterday (see link below), new problems have opened up.
First, the solution to the above problem works only for the IE. When I try chrome or firefox with the same code to feed (except, of course, the driver) I get back the error message as at the beginning (before I downgraded the IEDriverServer). see link.
Eclipse Java Selenium failed to connect to localhost
Secondly. does not happen when I use the findelement function. Error message "unable to find e
lement". however, this element is there. here the page that should be controlled.
https://accounts.google.com/signup/v2/webcreateaccount?continue=https%3A%2F%2Fwww.google.de%2F%3Fgws_rd%3Dssl&hl=de&flowName=GlifWebSignIn&flowEntry=SignUp
this problem is already frequently requested but unfortunately the answers do not help me. one solution was (as the findelement is faster than the browser) to install the following delay. driver.manage (). timeouts (). implicitlyWait (5, TimeUnit.SECONDS);
another solution was to search for id or xpath instead of name (that's why name and id are listed below as findelement). so far without success. none of the mentioned keys is entered in a field (password and first name, etc., I have already tried).
Here are the articles that come closest to my problem but have not helped me.
No such element exception | Internet explorer.
Same CSS Slector Not Working in Internet Explorer but works in firefox
anyway. the IE will open, but no other browser and no element will be filled. here code and error message.
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.grid.selenium.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;
public class firefoxöffner {
public static void main(String[] args) throws IOException, URISyntaxException {
System.setProperty("webdriver.gecko.driver","./geckodriver");
String service = "D://IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", service);
InternetExplorerDriver driver = new InternetExplorerDriver();
driver.get("https://accounts.google.com/signup/v2/webcreateaccount?flowName=GlifWebSignIn&flowEntry=SignUp");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement myDynamicElement = driver.findElement(By.id("lastName"));
driver.findElement(By.id("lastName")).sendKeys("B");
driver.findElement(By.name("lastName")).sendKeys("A");
}
}
Started InternetExplorerDriver server (64-bit)
3.8.0.0
Listening on port 47620
Only local connections are allowed
Mai 06, 2018 11:29:28 NACHM. org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMATION: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #lastName
Thanks again.
If you do Web Interface tests with Java + Selenium, I advise you to use the NoraUi Open Source Framework.
This Framework manage IE webdriver, Chrome webdriver and FF webdriver.
When ran on IE Probably //*[#id="lastName"] doesn't quite give you the exact element you want to work with. Instead when Can you try giving Xpath like below
//*[#id="lastName"]//following-sibling::div"

The driver executable does not exist: Selenium Firefox

I've been struggling with this for the past few hours. I'm trying to install Selenium web driver and have been running into a bunch of errors which prevent me from running the test page. I'm pretty sure my most recent issue is with this code:
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.gecko.driver","C:/Users/theone/Downloads/geckodriver 2.exe");
Would really appreciate any feedback on second steps!
package automationFramework;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstTestCase {
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.gecko.driver","C:/Users/theone/Downloads/geckodriver 2.exe");
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
//Launch the Online Store Website
driver.get("http://www.store.demoqa.com");
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
//Wait for 5 Sec
Thread.sleep(5);
// Close the driver
driver.quit();
}
}
You can setup Selenium with GeckoDriver either with webdriver.gecko.driver property or using environment properties. It would be good if you the latest version of Firefox, GeckoDriver and Selenium 3.0
Check out this article which provides setup using both these ways -
http://automationtestinghub.com/selenium-3-0-launch-firefox-with-geckodriver/
C:/Users/theone/Downloads/geckodriver 2.exe
There's a space in the path, it may work if you rename your file geckodriver2.exe.

Running Internet Explorer with selenium WebDriver.quit(); does not work

I am running the following code and the browser does not close.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class _getPageIE {
public static void main(String[] args) throws InterruptedException {
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
System.setProperty("webdriver.ie.driver", "C:\\WebDriver\\IEDriverServer.exe");
WebDriver ieDriver = new InternetExplorerDriver(capabilities);
ieDriver.manage().window().maximize();
ieDriver.get("http://google.com");
ieDriver.quit();
System.out.println("end");
}
}
The Browser opens, then is maximized and the Google page is displayed. Then the System.out.printlin is executed and the program ends, but the browser remains open.
I am running Eclipse with Selenium 2.44 on Windows 8.1 64 bit with the 64 bit IEDriverServer.exe.
The Console output is as follows.
Started InternetExplorerDriver server (64-bit)
2.44.0.0
Listening on port 46733
end
Any help is appreciated.

WebDriverException: Error forwarding the new session cannot find : {platform=WINDOWS, browserName=FIREFOX, version=3.6}

I am new to Selenium Web driver as well as Grid 2.
I am trying to run a test case but it gives me an exception
Exception in thread "main" org.openqa.selenium.WebDriverException: Error forwarding the new session cannot find : {platform=WINDOWS, browserName=FIREFOX, version=3.6}
I have started a node and hub using command
java -jar selenium-server-standalone-2.29.0.jar -role hub
java -jar selenium-server-standalone-2.29.0.jar -role node -hub %grid register%
Both the command works fine.
I am not sure when and where I need to use the command line
-browser browserName=firefox,version=3.6,maxInstances=5,platform=WINDOWS
(Tried to configure the node from Grid 2 official page
Is it because of this?
Here is my code:
package test;
import java.net.URL;
import java.net.MalformedURLException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Test {
public static void main(String[] args) throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setBrowserName("FIREFOX");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
capability.setVersion("3.6");
// capability.setCapability("");
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
//WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
For Registering Node, with a specific browserr configuration u have to use the below line from command prompt:
java -jar selenium-server-standalone-2.32.0.jar -role node -hub http://xxx.xxx.xxx.xxx:4444/grid/register -browser browserName=firefox
Replace the xxx with the actual ip address
I think it is due to capability.setBrowserName("FIREFOX");
should be capability.setBrowserName("firefox");
you can check url http://localhost:4444/grid/console , if already selenium grid is up and running but need to check whether any node is registered or not !!(i.e check any browser is register or not)
if not you need to register selenium node using below command
java -jar selenium-server-standalone-x.xx.0.jar -role node -hub http://localhost:4444/grid/register
make sure firefox browser is installed on the machine and geckodriver is present on the Path.
sometimes if you already running selenium hub using docker container you need to kill docker container using docker kill $(docker ps -q);
If you are running webdriverio then check docker selenium container running status .
I also faced the same issue.
It got solved,the problem was with the port 4444. which was blocked.
So creating a Global IP for my system and allowing port 4444 worked for me.
It is probably just what it says: the hub/selenium can not find a match for the requested capabilities.
I had this issue, and the error a got was (after formatting):
java.lang.RuntimeException : org.openqa.selenium.WebDriverException : Error forwarding the new session cannot find : Capabilities[{
proxy = {
proxyAutoconfigUrl = null,
socksUsername = null,
socksPassword = null,
autodetect = false,
httpProxy = xxxxxxxxxxxx.com : 8080,
proxyType = MANUAL,
noProxy = xxxxxxxxxxxxx.net,
ftpProxy = null,
hCode = 1273131486,
socksProxy = null,
class = org.openqa.selenium.Proxy,
sslProxy = xxxxxxxxxxxxxx.com : 8080
},
loggingPrefs = org.openqa.selenium.logging.LoggingPreferences # 3564e4e9,
browserName = MicrosoftEdge,
type = regular,
version = ,
platform = ANY
}
]
Turns out that my co-workers had added a new parameter to the capabilities ("type"), and I had not updated my .json file that configures my Selenium node.
If you run parallel test. Increate thread-count and increase hub memory
cat /proc/sys/kernel/threads-max
echo 100000 > /proc/sys/kernel/threads-max

Categories

Resources