How to run selenium webdriver on client machine? - java

I have implemented selenium web-driver with IE in a java web application.
I am using Apache tomcat 6 to run the application.
All the tests are running fine on local machine,but when i am trying to access it with other machine its opening the browser in the server machine and performing the tests.
My requirement is when any client access my application, the tests should run on the client machine, i mean browser should open on the client machine and do the tests.
my current selenium set up
File file = new File("C:/Jar File/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("ignoreZoomSetting", true);
caps.setCapability("nativeEvents", false);
driver = new InternetExplorerDriver(caps);
Any help will be appreciated, plz let me know if i am not clear.

If you are using selenium grid, use RemoteWebDriver instead of InternetExplorerDriver.
RemoteWebDriver: https://code.google.com/p/selenium/wiki/RemoteWebDriver
Selenium Grid: https://github.com/SeleniumHQ/selenium/wiki/Grid2
If you already registered a node on your hub, which has a registered Internet Explorer, then you can start your tests on it, if you init your WebDriver like this:
DesiredCapabilities dc = new DesiredCapabilities();
dc = DesiredCapabilities.internetExplorer();
dc.setPlatform(org.openqa.selenium.Platform.WINDOWS);
dc.setVersion("any");
WebDriver driver = new RemoteWebDriver(new URL("http://HUB_IP:HUB_PORT/wd/hub), dc);

Related

How to enable vpn in opera browser when selenium script is executed usingjava

System.setProperty("webdriver.opera.driver", "C:\folderNmae\operadriver.exe");
WebDriver driver = new OperaDriver();
Please add any information to do.

Creating Firefox profile and switching off the marionette

I am coming from Ruby background, I know how to do this in Ruby Selenium Binding, but I don't know how to do it Java Selenium Binding,
I have this code to create Firefox profile
FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
WebDriver driver=new FirefoxDriver(firefoxProfile);
It works in selenium 2.53 but it's throws error in very recent selenium binding 3.11.0, Can anyone tell me what's the alternative?
And also I wanted to switch off the marionette to connect to Legacy Firefox driver, I can do this with the following code
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", false);
WebDriver driver=new FirefoxDriver(capabilities);
But if I use the above line, then it gives the FirefoxDriver is deprecated. Can anyone guide me how to create profile as well as how to switch off the marionette?
Yes FirefoxDriver(desiredCapabilities) is deprecated.
Alternate way would be to go with options:
FirefoxOptions foptions = new FirefoxOptions(capabilities);
WebDriver driver=new FirefoxDriver(foptions);
Update : [In order]
FirefoxOptions foptions = new FirefoxOptions();
FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
foptions.setProfile(firefoxProfile);
foptions.setCapability("marionette", false);
foptions.setBinary("C:\\Program Files\\Mozilla Firefox 52\\firefox.exe");
WebDriver driver = new FirefoxDriver(foptions);
To use an existing Firefox Profile for your Test Execution first you have to create a Firefox Profile manually following the instructions at Creating a new Firefox profile on Windows. Now you have to pass the Firefox Profile to a FirefoxOptions class object. Additionally as you would be using the Legacy Firefox Browser
you have to set marionatte to false through a DesiredCapabilities class object which you need to merge() into the FirefoxOptions class object as follows :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(testprofile);
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("marionatte", false);
options.merge(dc);
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com");
Update
I am not sure about your usecase and why you want to use Legacy Firefox Driver. But as per the GitHub discussion Unable to Start Firefox Using the Legacy Driver on a 3.5.3 Grid #jimevans clearly mentions :
The legacy Firefox driver won't work with Firefox 53 or so. You might get the browser to launch, but the language bindings will be entirely unable to communicate with the driver (because Firefox will refuse to load the browser extension that is the legacy Firefox driver).
#barancev also mentions :
A binding should not pass OSS capabilities in W3C-compliant parts of payload, in "capabilities" block. They are allowed in "desiredCapabilities" block only. Perhaps, Mozilla broke Selenium compatibility in Firefox 48 in release channel, but restored it in version 52 in esr channel. It was unexpected, but it's true.
It's all upto you to take a informed descission.

How to implement the selenium on public browser

WebDriver driver=new FirefoxDriver();
It automatically open the Private firefox browser. Is there any option to open normal browser instead of Private browser.
Not without some serious work on your end. When you create a new WebDriver instance, a firefox instance is started that uses no user profiles. It's like a fresh installation. WebDriver installs a profile on this clean browser instance and installs an extension that runs a little micro web server that listens for instructions from your java program. This little server is what enables two way communication between the browser's javascript environment and your remote java program.
The relationship between the running instance of this firefox web server extension and the WebDriver instance running in your java program is a tightly controlled partnership. It's not part of their criteria to allow you to do what you're asking, so you would have to venture far outside the boundaries of what they support.
You can directly invoke the browser without giving setProperty in firefox for selenium versions <3.0
Firefox in Selenium 3
System.setProperty("webdriver.gecko.driver","path of the driver");
WebDriver driver = new FirefoxDriver();
Replace webdriver.gecko.driver with webdriver.firefox.marionette if above doesn't work.
Chrome browser
System.setProperty("webdriver.chrome.driver", "path of the driver");
WebDriver driver=new ChromeDriver();
IE browser
System.setProperty("webdriver.ie.driver","path of the driver");
WebDriver driver=new InternetExplorerDriver();
Headless Browser
WebDriver driver = new HtmlUnitDriver();

PDF is not being opened in Chrome started by Selenium

I am using Selenium and ChromeDriver 2.43.1 with the latest Chrome (Version 42.0.2311.135 at the time the quetion was asked). My web application generates a PDF. It is being sent with the correct MIME type and it also correctly opens in the Chrome PDF viewer. However when I try to open the PDF using Selenium in Chrome that is started by the WebDriver, it gets downloaded.
I believe it might be some settings that Selenium or WebDriver use to start Chrome.
I've tried settings a few switches, but nothing worked yet. My code:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add ChromeDriver-specific capabilities through ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.addArguments("--please-make-it-work"); // not a real switch
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
webDriver = new ChromeDriver(capabilities);
webDriver.get(url);
What I really need is to start the browser in "normal" mode. It doesn't need any profile settings, just the defaults that will open the PDF.
The problem was caused by the recent change to the behaviour of the --test-type switch. It is described in the ChromeDriver issue tracker.
The workaround is to disable this switch. Here's my code changed:
// Add ChromeDriver-specific capabilities through ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Arrays.asList("test-type"));
webDriver = new ChromeDriver(options);
webDriver.get(url);

Using Selenium 2 RemoteWebDriver with ChromeDriver

I searched for an answer to my question here and on the web but couldn't find anything that was helpful to me. Hopefully this isn't too dumb of a question.
I'm trying to get Selenium 2 to work using various browsers. I am using a Mac as a hub and a node and a Windows pc as a node. My problem is with Chrome. I want to initiate the Java code on the Mac and have the Selenium tests run on the Windows pc. To get Chrome to run on localhost I have the following code:
System.setProperty("webdriver.chrome.driver", "Users/xxxxx/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
This opens up Chrome on the hub/node Mac. How do I get it to open up on the Windows PC? Can I pass anything into the ChromeDriver() class?
I've tried using RemoteWebDriver, and have the following:
System.setProperty("webdriver.chrome.driver", "/Users/xxxxx/chromedriver");
DesiredCapabilities cap = DesiredCapabilities.chrome();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:9515/wd/hub), cap);
driver.get("http://www.google.com");
The code compiles and executes, but Chrome never comes up. I don't get any errors. Note that I'm initiating the RemoteWebDriver on localhost and Chrome still doesn't work. Nothing changes if I change the URL to the IP of the Windows PC. I'm either doing something wrong with the RemoteWebDriver or I need to pass parameters to ChromeDriver. Please help.
Found the answer after a bit more searching. Turns out that the URL of the remotewebdriver needed to be only localhost:9515 without /wd/hub. Also, if running on another machine, make sure to start up chromedriver on that machine and point webdriver.chrome.driver to the location of chromedriver.

Categories

Resources