EzDevInfo.com

selenium interview questions

Top selenium frequently asked interview questions

Is there a way to get element by Xpath using JavaScript in Selenium WebDriver?

I am looking for something like:

getElementByXpath(//html[1]/body[1]/div[1]).innerHTML

I need to get the innerHTML of elements using JS (to use that in Selenium WebDriver/Java, since WebDriver can't find it itself), but how?

I could use Id attribute, but not all elements have Id attribute.

[FIXED]

I am using jsoup to get it done in Java. That works for my needs. Thanks for the answers. :)


Source: (StackOverflow)

Scroll Element into View with Selenium

Is there any way in either Selenium 1.x or 2.x to scroll the browser window so that a particular element identified by an XPath is in view of the browser? There is a focus method in Selenium, but it does not seem to physically scroll the view in FireFox. Does anyone have any suggestions on how to do this?

The reason I need this is I'm testing the click of an element on the page. Unfortunately the event doesn't seem to work unless the element is visible. I don't have control of the code that fires when the element is clicked, so I can't debug or make modifications to it, so, easiest solution, scroll the item into view.


Source: (StackOverflow)

Advertisements

Casperjs/PhantomJs vs Selenium

We are using Selenium to automate our UI testing. Recently we have seen majority of our users using Chrome. So we wanted to know - pros and cons of using PhantomJS vs Selenium:

  • Is there any real advantage in terms of performance, e.g. time taken to execute the test cases?
  • When should one prefer PhantomJS over Selenium?

Source: (StackOverflow)

How to select/get drop down option in Selenium 2

I am converting my selenium 1 code to selenium 2 and can't find any easy way to select a label in a drop down menu or get the selected value of a drop down. Do you know how to do that in Selenium 2?

Here are two statements that work in Selenium 1 but not in 2:

browser.select("//path_to_drop_down", "Value1");
browser.getSelectedValue("//path_to_drop_down");

Source: (StackOverflow)

Webdriver Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms

I have box where I run tests. It seems like Jenkins would ssh in and execute commands described in the specific job that's running.

Here I am trying to run my Selenium Webdriver tests, but it tells me that I have errors in launching firefox. The end idea is to run the webtests on this box entirely, and also take some screenshots of errors.

I am using selenium-java-2.25.jar, firefox 10, linux OS.

The funny thing is that I can ssh in manually into the box, temporarily copy the magic cookie from another user on the box (to get an X tunnel), do an export DISPLAY=mydisplay:1.0, then launch my selenium tests using ant. And this will bring up firefox and the tests just fine.

There are various threads here that seem to have the exact same problem, and I think I've tried most of them. Here is what I've done:

  • Reboot the box, log back in with VNC.

  • Placed a bash script in Jenkins to run before running selenium tests. The bash script basically just does an export DISPLAY=mydisplay:1.0. It also executes xclock. This works as I can see xclock being displayed in the VNC.

  • iptables have been turned off

  • firefox is correctly located in /usr/bin/firefox

  • sshd_config shows having X11Forwarding as true.

  • Supposedly downgrading firefox helped some people, but I do not wish to do this. Webdriver should support FF 10 anyway.

However, none of the above solves the problem.

It doesn't seem like port 7055 on localhost even exists:

netstat -an | grep 7055 - Nothing prints

This is what my /etc/hosts says:

  1 127.0.0.1               localhost.localdomain localhost
  2 ::1             localhost6.localdomain6 localhost6

Perhaps it has something to do with localhost:7055 not existing? I am unsure where to go from here. still, why does the error output say it's looking for display: :0.0 when I've specified mydisplay:1.0?

And finally the error output I receive:

[testng] org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
   [testng] Xlib: connection to ":0.0" refused by server
   [testng] Xlib: No protocol specified
   [testng] 
   [testng] Error: cannot open display: :0.0
   [testng] Xlib: connection to ":0.0" refused by server
   [testng] Xlib: No protocol specified
   [testng] 
   [testng] Xlib: connection to ":0.0" refused by server
   [testng] Xlib: No protocol specified
   [testng] 
   [testng] Xlib: connection to ":0.0" refused by server
   [testng] Xlib: No protocol specified
   [testng] 
   [testng] Error: cannot open display: :0.0
   [testng] 
   [testng]     at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:109)
   [testng]     at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:245)
   [testng]     at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:109)
   [testng]     at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:185)
   [testng]     at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:178)
   [testng]     at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:174)
   [testng]     at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:92)
   [testng]     at com.test.webtest.browser.BrowserFactory.createBrowser(BrowserFactory.java:24)
   [testng]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   [testng]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   [testng]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   [testng]     at java.lang.reflect.Method.invoke(Method.java:601)


Source: (StackOverflow)

Any suggestions for testing extjs code in a browser, preferably with selenium?

We've been using selenium with great success to handle high-level website testing (in addition to extensive python doctests at a module level). However now we're using extjs for a lot of pages and its proving difficult to incorporate Selenium tests for the complex components like grids.

Has anyone had success writing automated tests for extjs-based web pages? Lots of googling finds people with similar problems, but few answers. Thanks!


Source: (StackOverflow)

Access to file download dialog in Firefox

Is there any kind of API that can allow me to manipulate a file download dialog in Firefox? (I want to access the one that appears when user does something, not initiate one myself).

What I want to do is to access this dialog from Selenium (and whether Selenium "privileged mode" is enough to access chrome interface is something I am not sure about as well).


Source: (StackOverflow)

Is it possible to hide the browser in Selenium RC?

I am using Selenium RC to automate some browser operations but I want the browser to be invisible. Is this possible? How? What about Selenium Grid? Can I hide the Selenium RC window also?


Source: (StackOverflow)

Selenium c# Webdriver: Wait Until Element is Present

I want to make sure that an element is present before the webdriver starts doing stuff.

I'm trying to get something like this to work:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));
wait.Until(By.Id("login"));

I'm mainly struggling how to setup up the anynomous function..


Source: (StackOverflow)

WebDriver (Selenium 2) API documentation [closed]

I can't locate an official class/method/properties type API reference for WebDriver anywhere, only the 5 minute guide and Next Step pages located at seleniumhq, plus a few other haphazard descriptions. Does one exist?


Source: (StackOverflow)

How to run Google Chrome with Selenium RC?

I am trying to test my site with Selenium RC + Nunit + asp.net.

I can get my tests to work in Firefox,Safari,opera,IE 8 but not google chrome. I put this in for the browser arugment

 selenium = new DefaultSelenium(Server, ServerPort,
                                          "*chrome", DomainURL);

This always loads up Firefox though and not good chrome.


Source: (StackOverflow)

How do I maximize the browser window in Selenium WebDriver (Selenium 2) using C#?

Is there a way to maximize the browser window using WebDriver (Selenium 2) with C#?


Source: (StackOverflow)

Selenium IDE - Command to wait for 5 seconds

I´m using the Selenium IDE for Firefox and searching for a wait command. My problem is that I want to test a website with a embedded external map. This external map needs 3-5 seconds to load.

My commands:

open /Page/mysite.html
//Wait Command? (5 seconds)
ClickAndWait link=do something

Source: (StackOverflow)

Take a screenshot with Selenium WebDriver

Does anyone know if it's possible to take a screenshot using Selenium WebDriver? (Note: Not Selenium RC)


Source: (StackOverflow)

Headless Browser and scraping - solutions [closed]

I'm trying to put list of possible solutions for browser automatic tests suits and headless browser platforms capable of scraping.


BROWSER TESTING / SCRAPING:

  • Selenium - polyglot flagship in browser automation, bindings for Python, Ruby, JavaScript, C#, Haskell and more, IDE for Firefox (as an extension) for faster test deployment. Can act as a Server and has tons of features.

JAVASCRIPT

  • PhantomJS - JavaScript, headless testing with screen capture and automation, uses Webkit. As of version 1.8 Selenium's WebDriver API is implemented, so you can use any WebDriver binding and tests will be compatible with Selenium
  • SlimerJS - similar to PhantomJS, uses Gecko (Firefox) instead of WebKit
  • CasperJS - JavaScript, build on both PhantomJS and SlimerJS, has extra features
  • Ghost Driver - JavaScript implementation of the WebDriver Wire Protocol for PhantomJS.
  • new PhantomCSS - CSS regression testing. A CasperJS module for automating visual regression testing with PhantomJS and Resemble.js.
  • new WebdriverCSS - plugin for Webdriver.io for automating visual regression testing
  • new PhantomFlow - Describe and visualize user flows through tests. An experimental approach to Web user interface testing.
  • new trifleJS - ports the PhantomJS API to use the Internet Explorer engine.
  • new CasperJS IDE (commercial)

NODE.JS

  • Node-phantom - bridges the gap between PhantomJS and node.js
  • WebDriverJs - Selenium WebDriver bindings for node.js by Selenium Team
  • WD.js - node module for WebDriver/Selenium 2
  • yiewd - WD.js wrapper using latest Harmony generators! Get rid of the callback pyramid with yield
  • ZombieJs - Insanely fast, headless full-stack testing using node.js
  • NightwatchJs - Node JS based testing solution using Selenium Webdriver
  • Chimera - Chimera: can do everything what phantomJS does, but in a full JS environment
  • Dalek.js - Automated cross browser testing with JavaScript through Selenium Webdriver
  • Webdriver.io - better implementation of WebDriver bindings with predefined 50+ actions
  • Nightmare - Electron bridge with a high-level API.
  • jsdom - Tailored towards web scraping. A very lightweight DOM implemented in Node.js, it supports pages with javascript.

WEB SCRAPING / MINING

  • Scrapy - Python, mainly a scraper/miner - fast, well documented and, can be linked with Django Dynamic Scraper for nice mining deployments, or Scrapy Cloud for PaaS (server-less) deployment, works in terminal or an server stand-alone proces, can be used with Celery, built on top of Twisted
  • Snailer - node.js module, untested yet.
  • Node-Crawler - node.js module, untested yet.

ONLINE TOOLS


RELATED LINKS & RESOURCES

Questions:

  • Any pure Node.js solution or Nodejs to PhanthomJS/CasperJS module that actually works and is documented?

Answer: Chimera seems to go in that direction, checkout Chimera

  • Other solutions capable of easier JavaScript injection than Selenium?

  • Do you know any pure ruby solutions?

Answer: Checkout the list created by rjk with ruby based solutions

  • Do you know any related tech or solution?

Feel free to reedit this question and add content as you wish! Thank you for your contributions!


Updates

  1. added SlimerJS to the list
  2. added Snailer and Node-Crawler and Node-phantom
  3. added Yiewd WebDriver wrapper
  4. added WebDriverJs and WD.js
  5. added Ghost Driver
  6. added Comparsion of Webscraping software on Screen Scraper Blog
  7. added ZombieJs
  8. added Resemble.js and PhantomCSS and PhantomFlow, categorised and reedited content
  9. 04.01.2014, added Chimera, answered 2 questions
  10. added NightWatchJs
  11. added DalekJS
  12. added WebdriverCSS
  13. added CasperBox
  14. added trifleJS
  15. added CasperJS IDE
  16. added Nightmare
  17. added jsdom
  18. added Online HTTP client, updated CasperBox (dead)

Source: (StackOverflow)