selenium rc on one linux server - selenium

I want to have selenium to run on one server like ubuntu, centos and to run all browsers check on that linux (centos or ubuntu server). So check of ie6, ie7, ie8, ie9, chrome, firefox etc.
But then I think this is not possible, because for ie we need windows machine.
Or if we remove the ie and only want to test on chrome and firefox, can we do that on selenium rc on ubuntu or centos? Then I think on that server version I need to install firefox.
I think the main thing is that I don't get how the selenium server can work with actually not having browser installed or it can't?
Can anyone give me some instruction on this, I did read some documentation and nice tutorials, but this is not very clear to me.

Selenium Server is just an application that can send commands to web browsers. But, of course, you need a browser for that. If there's no browser and you write your tests in Selenium 2 (WebDriver), you can use HtmlUnitDriver (JavaDoc) which is inbuilt and doesn't actually open any browser. You could have read about it as "the in memory browser".
You could also check for presence of the browser by possibly doing something in the way of
WebDriver driver;
try {
driver = new InternetExplorerDriver();
catch (WebDriverException e) {
System.out.print("IE not found.");
try {
driver = new FirefoxDriver();
} catch (WebDriverException e) {
System.out.print("FF not found.");
}
// etc.
}

Related

Selenium grid 4: How to run a test with differents browsers and OS

Beginner on selenium grid, I just created a small test that allows to navigate on the home page of stackoverflow
For this I placed the selenium server on my disk, I opened 2 terminals as shown in the documentation here.
https://www.selenium.dev/documentation/grid/getting_started/#hub-and-nodes
The test starts well, and now I would like to run the same test under firefox while modifying the OS (for example: windows). How do I do this? Do I have to create another test file in my project? And then how to run the test with multiple configurations? I can’t find an answer to these questions.
My configuration:
-Linux Ubuntu 20.04
-Google and chrome 95
-the last version selenium server : 4.1.1
Here's the code of the test:
public class StepGoStackoverflow {
RemoteWebDriver driver;
String nodeUrl;
#Given("I'm on google search page")
public void i_m_on_google_search_page() {
try {
nodeUrl = "http://localhost:4444";
ChromeOptions options = new ChromeOptions();
// options.addArguments("--headless");
options.addArguments("start-maximized");
driver = new RemoteWebDriver(new URL(nodeUrl), options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.get("https://www.google.com");
} catch (MalformedURLException error) {
error.printStackTrace();
}
}
#When("I enter the name of the site")
public void i_enter_the_name_of_the_site() {
WebElement webElementList = driver.findElement(By.id("L2AGLb"));
webElementList.click();
driver.findElement(By.name("q")).sendKeys("stackoverflow", Keys.ENTER);
}
#Then("I'm navigated on the home page of stackoverflow")
public void i_m_navigated_on_the_home_page_of_stackoverflow() {
driver.findElement(By.xpath("//a[#href='https://stackoverflow.com/']")).click();
driver.close();
}
}
EDIT:
I forgot to give the snipet of Gerkhin:
Feature: search the home page of stackoverflow
Scenario: Go to the site stackoverflow
Given I'm on google search page
When I enter the name of the site
Then I'm navigated on the home page of stackoverflow
Thank you
I can see that you are using Cucumber so I edited your question to add corresponding tag (since that matters as we're taking about parameterization).
Grid part
The idea of the Selenium Grid is that you install Grid components at a cluster where the nodes represent different OSs and each runtime/OS has one or more drivers and browsers installed.
So you configure nodes so that they know where are drivers installed and how to run browsers (and what browser can each node execute: e.g. Chrome, Firefox) and each node registers at the grid component called hub.
On one side a hub knows which nodes are running at which OS and what browsers they can operate with. On another hand hub acts as remote web driver (it exposes webdriver protocol to external clients aka automated tests).
You can find an overview of the grid here and find configuration flags and aspects here.
Code part
On the code side you parameterize your tests so that each test is executed from scratch but with different capabilities set up. In your example you only use ChromeOptions which would make hub to dispatch your calls to a node that can run Chrome browser.
Your challenge is to make your code configure different capabilities each run so that Grid will look up appropriate node in the cluster for your test (like "ah-ha.. you want to run Firefox on Linux which means I'll dispatch your calls to node C")
Cucumber
Since you are using Cucumber in your example I assume that you tend to incorporate it into final solution. If so, you need to start from learning parameterization practices in cucumber: See here, here and here; setting up context in Cucumber, and sharing the state in Cucumber with Dependency Injection with Guice or PicoContainer.
Combine everything, add a handful of parallelization and you will gain the most value from your framework.

Why do we need to set the System Property for Chrome And IE Browser and Not For Firefox Browser

For Chrome,
public class Chrome {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
}
}
for Firefox,
public class Firefox {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
Why do we need to specify the system.setProperty for Chrome and IE?
I had also same question, but after digging I found,
WebDriver uses native browser approach. Selenium offers inbuilt
driver for Firefox but not for other browsers. All drivers (Chrome
Driver, IE driver, etc.) are built based on the special JS Engine used
by each browser.
Selenium WebDriver works very well with Mozilla Firefox because it has a built in driver server. But the same is not true for Internet Explorer and Google Chrome. Firefox is the most traditional browser, thus Selenium WebDriver do not require any additional utility to be set before launching the browser. The Selenium package automatically references towards the default location of the firefox.exe, thus the user need not to set any other property.
If you ever get the “the path to the driver executable must be set by the webdriver. ie. driver system property” error or its similarly worded Chrome equivalent, it means that you need to install the driver servers on your browser. The driver server manages the calls between the browsers and the Selenium wire protocol.
The InternetExplorerDriver is a standalone server which implements WebDriver’s wire protocol
Similarly, Google Chrome doesn’t have a built-in server so you will need a Chrome driver server for communicating your Selenium code to the browser. You can download the Chrome driver server.
Founded from here.
Implementation of FirefoxDriver, ChromeDriver, InternetExplorerDriver is different, thus the way of instantiating the object differs as well.
The Firefox Driver controls the Firefox browser using a Firefox plugin. The Firefox Profile that is used is stripped down from what is installed on the machine to only include the Selenium WebDriver.xpi
The InternetExplorerDriver is a standalone server which implements WebDriver’s wire protocol.
The ChromeDriver is maintained / supported by the Chromium project iteslf. WebDriver works with Chrome through the chromedriver binary (found on the chromium project’s download page). You need to have both chromedriver and a version of chrome browser installed. chromedriver needs to be placed somewhere on your system’s path in order for WebDriver to automatically discover it. The Chrome browser itself is discovered by chromedriver in the default installation path
For more details, refer the selenium documentation
Simple answer is, each browser has its own WebDriver implementation and is not maintained by the Selenium project. Hence for selenium to interact with the browser specific driver we need to specify the full path of the driver.
Why for firefox there is no need to specify the driverpath? In Selenium 2.0, selenium RC is still present and was supporting firefox. From Selenium 3.0 onwards there is no official support for any browser specific drivers. Hence, we need to specify the driver path through System.setproperty for all the browsers.

No Alert Found using Chrome Driver

Using ChromeDriver 2.14, selenium server 2.47.1 and Chrome 45. I am attempting to handle a basic authentication prompt. I have tried the following code to try and resolve this.
var wait = new OpenQA.Selenium.Support.UI.WebDriverWait(Driver.Value, new TimeSpan(0,0,60)).Until(OpenQA.Selenium.Support.UI.ExpectedConditions.AlertIsPresent());
Driver.Value.SwitchTo().Alert().Dismiss();
And this
Driver.Value.SwitchTo().Alert().SetAuthenticationCredentials("test", "test");
and this
while (true)
{
try
{
Driver.Value.SwitchTo().Alert().SetAuthenticationCredentials("test", "test");
break; //this is brute force I know
}
catch
{
Thread.Sleep(100);
}
}
No luck, they all throw a "no alert found" exception. We would switch to firefox, but it is an internal application and we only support IE or Chrome.
I didn't find a way to use the authentication options inside Selenium with the basic authtication prompt. Instead I found that if you run fiddler in the background, you can have it auto-authenticate for you when prompted based on a specific site. Then the popup never shows up and your Selenium scripts run just fine.
Auto http authenticate traffic for a specific site with fiddler

where does selenium run?

I am super confused with selenium.
Completely new to automated testing and struggling to get a foothold with selenium.
http://www.seleniumhq.org/projects/webdriver/
I get the concept of writing tests and have done a few with in angular with protractor, but i need to use selenium for a particular project and not sure where to start.
To start with I just want to be able to write some simple client side tests, but i have no idea where to start with on Selenium, i read their docs but not really any the wiser..
A webdriver test is just a script, written in one of many compatible languages (java, python, etc). The script runs on any machine. During development it will typically run on your local machine, but eventually your test could run on a continuous integration server.
Webdriver has two modes of operation: it can open a browser on the same machine that the script is running, or it can send a request to a selenium grid hub, which will open a browser on one of its nodes.
As a simple example, here is a complete working example of a selenium test in python (taken from python selenium bindings Getting Started page):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
If you have python and the selenium libraries installed, you can save this file to "example_test.py" and then run it from a command prompt with python example_test.py
Here's a similar test, this one in javascript, taken from the WebDriverJS User's Guide. You would run it the same way you run any javascript program.
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
return driver.getTitle().then(function(title) {
return title === 'webdriver - Google Search';
});
}, 1000);
driver.quit();
To give you an organic answer to your question, when you run a selenium Firefox browser test, it starts a local ad-hoc "Selenium grid hub listener" on a port like 30005 or something at the start of the test. Then, the code that you write talks to that local port in JSON format by sending local http requests to localhost:30005 . The "Grid hub" listening on that port knows how to talk to your local web browser and controls it by answering commands passed through that port. At the end of the test, the "selenium grid hub listener port" closes.
If you do some reading and try to understand how the "Selenium Grid Hub" works and also learn about "WebDriver Wire Protocol", then it might help you start understanding what I explained above.
Always read the official Selenium docs for references, but if you are using Java, you can get a head-start by using the Conductor framework
It's a Java specific DSL, so won't work with any other.
A test would look like this:
#Config(url="http://google.com", browser=Browsers.CHROME)
public class MyTest {
#Test
public void testGoogle() {
// a quick google search
setText(By.name("q"), "something")
.click("[name='btnG']");
}
}

Is it possible to automatically open Google Chrome devtools?

Given that Selenium 2 closes the devtools window which contains my emulator profile saved under my user profile for chrome. Is there a way to trigger devtools to open using a selenium script?
Here is the info on the devtools window closing issue
https://sites.google.com/a/chromium.org/chromedriver/help/devtools-window-keeps-closing
I feel a little exhausted trying some of these Chromium override parameters only one of which seems to work
http://peter.sh/experiments/chromium-command-line-switches/
The one that had any affect is the following
options.addArguments("user-data-dir=/Path/to/chrome/profile");
If there is no way to open the dev tools window or panel, is there a way to initialize the emulator?
at the terminal, type the following -
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --auto-open-devtools-for-tabs
You can use Robot class for same. It just going to help you to open dev-tools on any browser.
Use this code on the place you want to open dev-tools
try{
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_F12);
robot.keyRelease(KeyEvent.VK_F12);
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
You can used below given code, it is working fine with FireFox just change the browser
public void open(String url) {
driver.get(url);
Actions action = new Actions(driver);
String pressF12=Keys.chord(Keys.F12,"");
driver.findElement(By.id("lst-ib")).sendKeys(pressF12);
}
As the same link you provided says, it's not possible to use ChromeDev tools with Selenium's ChromeDriver since 2.x. It's simply in direct conflict, you could use either one or the other.
The only way to perform a workaround is to pause all chrome driver interactivity, launch the DevTool's through some other automation framework such as Robots API, do whatever you need, and continue.
But I might think on what I need from dev tools, isn't there an alternative to what you need by providing Chrome with the proper configuration on launch? (loading a different profile, or loading an emulated device)
Why do you need the chrome devtools anyway?
Is it for monitoring the network traffic? Why not usefirefoxdriver with firebug.xpi or browsermobproxy.
To open a page in emulated device's browser. This can be done with chrome, without opening the devtools.
WebDriver with chrome emulated browser
This is a code snippet that you can use to fire up chrome browser in a particular emulate version. Note the key thing here is the deviceName value, which should match as it is mentioned in the chrome browser of yours.
public static WebDriver MobileOpen()
{
Map<String, String> mobileEmulation = new HashMap<String, String>();
System.setProperty("webdriver.chrome.driver","path/to/chromedriver");
mobileEmulation.put("deviceName", "Google Nexus 6");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver = new ChromeDriver(capabilities);
return driver;
}
Hope this helps.
Edit: I just saw it is a 3 years old post. The guy probably has found his way home. :P
Have you tried the console API for your needs? It will allow you to invoke some dev-tool functions from inside JS.