I'm using the Java API of the Selenium WebDriver:
Is it possible to create multiple instances of the Selenium WebDriver from different threads simultaneously without conflict?
How do I change the path of the firefox installation directory that WebDriver uses if I installed firefox in a different directory?
How can I hide all the instances of the browsers(e.g firefox) that those threads started?
Thank you.
I can give you an answer to your first question.
Yes, you can run multiple driver instances simultaneously. However it is not recommended to run more than 5 or so instances at once in a single selenium server. Selenium Grid was designed specifically for this (it is bundled with the Selenium Server).
Related
From within my app I want to hit a website and than perform user action. Currently am using webview but I think webdriver will be easy to use and correct approach.
Current code:
WebView browser = (WebView) view.findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
browser.getSettings().setUserAgentString(`"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.81 Safari/537.36");`
browser.setWebViewClient(new MyBrowser());
browser.loadUrl("https://myurl.com");
Issues with current code: Its hard to send key stores or use Xpath.
What am looking for?
Hit the website using driver and than click buttons etc. Pseudo code as follows:
chat=driver.find_element_by_xpath("/html/somepath")
chat.click()
time.sleep(2)
search=driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/div[1]/span/div/span/div/div[1]/div/label/input")
search.click()
I understand that this is possible using selenium/appium. But what am confused is does selenium/appium also needs a server that runs on a separate machine? I want to run all of the code in my app without external server or any more apps.
Can I just add lib which gives me access to apis like I showed above?
You can use the Selenium or Appium without any server. Both are plugins, which means they are basically open code or libraries. You call those objects on your local machine (or phone), you don't call an online remote API.
The Selenium and Appium helps to find elements on a web page or to find elements inside a mobile app. There is absolutely no need for a server here or remote machine.
So, YES, just add the lib which gives you access to api's like you showed above.
Selenium
Selenium in it's basic form doesn't needs any seperate server to run. Selenium along with it's wide range of tools and libraries that can support the automation of web browsers within the same machine ( i.e. localhost).
WebDriver
At the core of Selenium is WebDriver an interface to write instruction sets that can be run interchangeably in many browsers using each browser's native support for Test Automation. This can be achieved in three simple steps:
Installing the Selenium library
Installing the browser drivers
Writing your first Selenium script
Sample code block:
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.com/");
driver.findElement(By.cssSelector("input.nav-input[value='Go']")).click();
Selenium Grid
However, Selenium also supports a distribution server for scaling browser allocation. If your usecase include steps to run tests in parallel across multiple machines, then Selenium Grid would be your best bet.
Selenium Grid allows the execution of WebDriver scripts on remote machines (virtual or real) by routing commands sent by the client to remote browser instances. It aims to provide an easy way to run tests in parallel on multiple machines.
Selenium Grid would also allow you to run tests in parallel on multiple machines and to manage different browser versions and browser configurations centrally (instead of in each individual test).
Having said that, it does solves a subset of common delegation and distribution problems, but will may not be able to manage your infrastructure, and might not exactly suit to your specific need.
Appium
Similarly, Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS mobile, Android mobile, and Windows desktop platforms. Hybrid apps which have a wrapper around a webview is a native control that enables interaction with web content. Projects like Apache Cordova make it easier to build apps using web technologies that are then bundled into a native wrapper, creating a hybrid app.
Based on your question and your response comment to the answer provided by #undetectedSelenium, the following assumptions apply:
You are testing a browser within an android phone that is connected to a Windows machine via an adb server running on the Windows machine
The browser under test is Chrome
Install selenium as part of your project:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
Sample code block based on your psuedo code and answer provided by #undetectedSelenium
System.setProperty(“webdriver.chrome.driver”, “C:\\path\\to\\chromedriver.exe”);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption(“androidPackage”, “com.android.chrome”);
// By default if the following option is not applied, selenium will take the 1st available
// node provided by the adb server if multiple android devices are attached
options.setExperimentalOption("androidDeviceSerial", deviceId);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.amazon.com/");
driver.findElement(By.cssSelector("input.nav-input[value='Go']")).click();
The deviceId variable needs to contain the uuid listed as a device from the adb server for the particular device under test, i.e.
options.setExperimentalOption("androidDeviceSerial", 95s572sp0478);
Also you will require the correct Chromedriver for your android device. Check the version of Chrome browser installed on the device and download the correct driver from here for your Windows machine Chromedriver Downloads. Then place into your desired directory and add the directory path into the code.
I use Selenium every 5 years or so and everytime it has changed beyond recognition. I just started a new Selenium project, googled some quickstart guides such as https://www.toolsqa.com/selenium-webdriver/run-selenium-test/ (written in September 2020) and https://www.guru99.com/first-webdriver-script.html (© 2020) and both seem to use WebDriver, e.g., by initiating their examples with WebDriver driver = new FirefoxDriver(); although the latter has a disclaimer saying that from Firefox 35 (I have 82) and up you should use Geckodriver.
I use Selenium for Java 3.141.59 downloaded from https://www.selenium.dev/downloads/ but it only has two references to Geckodriver (at least that is all that is displayed when it enter Ge and autocomplete in my IDE), GeckoDriverInfo and GeckoDriverService (as a comparison, there are nine references to WebDriver).
I have read the information here https://github.com/mozilla/geckodriver but it didn't make me any wiser, nor did https://en.wikipedia.org/wiki/Selenium_(software)#Selenium_WebDriver (Geckodriver isn't even mentioned on this Wikipedia page).
What is the difference between Webdriver and Geckodriver?
Why, if one download the newest/current version of everything, isn't Geckodriver included if that is the recommended tool since several (?) years?
Why are guides that are written recently use Webdriver if Geckodriver is the way to go?
I think I have done a reasonably amount of research before asking this question but feel free to suggest improvements because I am genuinely confused.
WebDriver is a specification. It defines the way how UI interfaces can be automated. GeckoDriver is the implementation of such specification - it is the WebDriver implementation for Firefox browser.
So basically a WebDriver is a server that exposes REST API to one side and that knows how to control browser on another side.
Here is short explanation of E2E flow (for Firefox and Java):
You download Selenium java library. It provides Java client for interacting with web drivers
You download GeckoDriver
In your Java code you call WebDriver driver = new FirefoxDriver();
Selenium library starts GeckoDriver executable in OS native manner
In your Java code you call driver.get("http://my.url")
Selenium library forms REST call to the server that is started with GeckoDriver. It invokes the endpoint according to this section of specification.
GeckoDriver then translates this command to somewhat that Firefox understands so that the browser navigates to required page.
So basically you need 3 things to make everything work:
Selenium Java library that is basically a Java client for WebDriver REST API
GeckoDriver (that implements REST API according to WebDriver specification and translates it to commands which Firefox browse can understand)
Firefox browser
webdriver, is the parent of ChromeDriver ( from chrome ), GeckoDriver ( FireFox ), IEDriver and RemoteDriver. Possibly even more if they are supported. So, the GeckoDriver is used to control a FireFox Browser instance, but it implements the methods mentioned in the WebDriver interface.
GeckoDriver is not included as it is specific to FireFox only, other users may want to use other browsers.
To keep the flexibility of swapping out the implementations for different browsers. :)
I'm planning to use Selenium Chrome Driver for my project which will be used to do web scraping to multiple public websites (something like kayak or skyscanner). So there will be a REST GET endpoint where my backend would launch headless Chrome to scrape multiple websites, and eventually return a manipulated JSON.
I want to know how scalable is Chrome Driver as it sounds like a headless Chrome instance needs to be launched whenever a request comes in.
Updated: Question using Google Chrome Headless
Please find the pros and cons of phantom js which I noticed during implementation .Hope this helps.
Cons:
1)It will fail to recognize the browser elements like id,xpath,csselector
when compared to chrome driver.
2)If you have login mechanism ,redirects won't work as you expect when compared to chrome driver.
3)You need to manually implement the custom logic for screen shots for the test failures if you need it.
4)If you want to switch between multiple drivers like chrome,html etc then it is very difficult
Pros:
1)Test case execution is faster when compared to chrome driver
2)No browser is required it will run without GUI.
3)No much configurations are needed when compared to chromedriver.
You can go with html driver also which is quite faster then phantom but even it has its own limitations that you need take care of before implementation.
I am not sure that you really need to use PhantomJS.
Chrome implemented "headless" mode couple of months ago.
"Headless Chrome" does the same job that PhantomJS, and does it better.
I heard that PhantomJS authors even said that they will not support it anymore.
You can enable headless mode in Selenide with just on line:
Configuration.headless = true;
Did you think about headless chrome?
Headless Chrome
I got into a project which requires a WebGUI to be tested. Selenium is used for this in combination with Jenkins to schedule tasks and run the tests. All of this is handled by one server (Linux).
Regular tests work fine but WebGUI tests fail and I am supposed to make them work. However I'm very confused by the roles of Jenkins' Plugin "Selenium Grid" (is it even neccessary), the JUnit tests inside the project utilizing a selenium libary and the setup of the headless browser required.
What exactly are the roles of the plugin "Selenium Grid" for Jenkins and how does the Selenium libary communicate with a headless browser (Role of WebDrivers?)?
I already know about the Selenium hub and nodes but have no idea how to approach this.
I hope somebody can help me cleaning up my confusion...
A Selenium Grid can be used to set up a scalable automated browser environment. It is often used for automated GUI testing. It consists of a Hub and one or more Nodes.
A Selenium Hub should be started and the resulting URL should be stored.
Then a desired number of Selenium Nodes should be started with a reference to the Url of the Hub. The nodes will register themselves to the Hub.
Then you could use Selenium Java to create a RemoteWebDriver instance, optionally specifying the url of the Selenium Hub. This webdriver is just a Java API, the actual work will be done in the Selenium Node environment(s).
You can use the Java API to implement your desired operations on the WebDriver instance. When running, the code will delegate all calls to the running Selenium environment.
There are multiple ways to actually start the hub and nodes. A Jenkins Plugin could do it, you could use Docker, a local process, or many more. I'd advise to not bind your setup directly to Jenkins unless you are certain that you will never switch.
The Jenkins Selenium Plugin specifies the URL that you should create a WebDriver with:
new RemoteWebDriver(new URL("http://jenkins.mydomain:4444/wd/hub"), capability);
Where jenkins.mydomain will probably be the IP of your Linux server. Just above it is a line that says that it "also" accepts external nodes. I am not familiar with the plugin, so I dont know whether this means that it sets up one or more nodes itself by default.
taken from the following website.
https://wiki.jenkins.io/display/JENKINS/Selenium+Plugin
This plugin sets up Selenium Grid in the following way
On master, Selenium Grid Hub is started on port 4444, unless configured otherwise in Jenkins global configurations. This is where all your tests should connect to.
For each slave, necessary binaries are copied and Selenium RCs are started.
RCs and the Selenium Grid Hub are hooked up together automatically.
Now the question is do you need this?
This is upto the requirement of the project. You just need to ask this question to yourself
do you need to run tests in parallel (to cutdown time or for browser compatibility testing), either now or in near future.
I am having old test automation framework developed using Selenium 1.0 and now wants to migrate my code to WebDriver.
Is there any easiest method to do this migration?
I have overridden most of the methods such as type, click, getText, getSelectedLabel, assert etc etc. I see the only method is to re-write all methods again from scratch, I have already started this process but If I continue with the same method It will take many days for me.
Please suggest if anyone has any better approach.
Thanks in advance.
They are completely different technologies. There is no way to migrate them over to selenium 2 per se.
Luckily, the recent Selenium releases have implemented what's called "WebDriver Backed Selenium" so technically if you are using those tests, it's implicitly running them "as" WebDriver tests.
Other than that, no, there is no easy way.
I had the same issue - we are migrating our entire regression suite over to S2 now :)
In the Webdriver documentation, they explain a method to start migrating from Selenium RC to Selenium WebDriver.
Basically, is creating the selenium object like this:
WebDriver driver = new FirefoxDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.yoursite.com");
the main problem with this migration (instead of changing the whole code) is the wait for page to load. As they say, the command WaitForPageToLoad returns too soon. The getEval is another command that you have to change.
I think that the best approach is to make functions with the main commands that differs from Selenium RC to Selenium WebDriver, and, once everything is "working" keep modifying your code until no Selenium RC is present. This is how we did the migration, and we had lots of lines of code.
This is the link, where they explain how to start:
http://www.seleniumhq.org/docs/appendix_migrating_from_rc_to_webdriver.jsp#migrating-to-webdriver-reference