Category Archives: FAQ

How to Select a Web Element with an id containing ‘.’

TLDR: \.

Lesson learned.

I was working with a site and trying to select an element via its id e.g.

<p id="select.me.by.id">Select Me By My id</p>

Simples thought me:

  • #select.me.by.id

Nope.

So I did what all good Selenium debuggers do… I tried in the browser but Chrome didn’t match it.

Tempted though I was to blame Chrome, I tried with XPath, and that worked fine:

  • //p[@id='select.me.by.id']

After a bit of web searching, the solution was clear.

In CSS I wasn’t saying, find the id select.me.by.id

I was saying find the id select with class me by id which was not at all what I wanted.

Fortunately CSS offers escaping, which I didn’t realise.

Until the app is fixed I can locate the id using CSS with:

  • #select\.me\.by\.id

Posted in FAQ, Selenium Simplified, Selenium Simplified Blog | Leave a comment

How to use the Firefox Marionette Driver

Note: This post was triggered by a blog post on David Burns’ Blog which states that FirefoxDriver will stop working at version 47 of Firefox. 

It is easy to switch to using the new MarionetteDriver.

  • I suggest you try it on your code, and if your tests work, then keep using it.
  • If your tests fail, then it is most likely some defects in the newer MarionetteDriver. In which case:

Steps to switch:

  • Download the marionette driver
  • Unarchive it to a folder of your choice
  • In your code set the "webdriver.gecko.driver" property to the location of the .exe
  • Use the MarionetteDriver

The above is slightly different from the official instructions on the page.

The official instructions suggest you:

  • rename the file to ‘wires.exe’ and
  • add it to your path.

I did rename it to wires.exe, mainly to keep the code consistent so I wouldn’t have to update the code everytime I change the version of the marionette driver.

But I didn’t add it to the path. I’d rather control that in the code when the option is provided.

So for Firefox 45 and 46 my start driver code looks like this:

WebDriver driver = new FirefoxDriver();

And for Firefox 47 and above my code looks like this:

String currentDir = System.getProperty("user.dir");
String marionetteDriverLocation = currentDir + "/tools/marionette/wires.exe";
System.setProperty("webdriver.gecko.driver", marionetteDriverLocation);
WebDriver driver = new MarionetteDriver();

I have a working example of this in the startUsingSeleniumWebDriver code on github:

Posted in FAQ, Selenium Simplified Blog, WebDriver | 30 Comments

Question: Do you use WebDriverWait instead of Asserts?

I watched some of your lectures and webinars and it looks like you are using WebDriverWait partly as a substitute for assertions. Is that normal?

WebDriverWait will throw an exception if it can’t find the item. Which has a similar effect to an Assert i.e the @Test will fail. I do not view this as an assert, I view this as setting up the necessary preconditions for the test, rather than a specific ‘check’ that the @Test enforces.

I explicitly use Asserts when the Assert describes something important for the test. So when people read the test they know what I am ‘testing for’.

Sometimes I add an Assert after a WebDriverWait, even though I know the Assert will never be thrown if the WebDriverWait fails, because in the context of the code, the Assert documents something important for the test.

I do not create tests without Asserts in production, even if a wait could act as an Assert.

Upon review I’d consider the test unclear, and I’d have to add a comment saying “no assert because the wait will fail”. Also it would mean a reliance on abstraction layers or Waits, but the system may change over time and the abstractions and necessary Waits may also change. But since I encode the test rationale in the Asserts, if all the Asserts become irrelevant and we remove them, then we also remove the need for the @Test itself.

Also I don’t add asserts to my abstraction layers, but I do add WebDriverWaits.

Semantically they differ, even if the end result of using them might seem the same.

Posted in FAQ, Selenium Simplified Blog | 1 Comment

Question: Which Fitnesse framework should I use with Selenium?

We have a Fitnesse server set up and use Selenium 2 (webdriver). Can you please suggest which framework we should use? Xebium, Selenesse, Fitnium, webtest? or Please suggest any other tool you would recommend.

Thanks for the list of Fitnesse addons.

But, I don’t use them.

The addons provide the ability to encode Web Actions in FitNesse tables. But I don’t do that.

I don’t think that Actions in the FitNesse table lead to maintainable FitNesse documents.

When I work with FitNesse I want to write FitNesse tables at a domain abstraction, not at a physical implementation abstraction. i.e. I want to use it as framework to create a Domain Specific Language, and not use it as a Web Automation Scripting language.

For example:

I do not want to encode tables that look like

| start browser | firefox |
| open url | www.seleniumsimplified.com |
| click on | //a[@id='buy course']|
| verifyTitle | Woo hoo |

I’d rather write tables that look more like:

| A visitor can | buy course | Selenium WebDriver|
| and then | buy book | Java For Testers|

The tables should work at the domain of the test and the reader, not the domain of the application implementation.

By doing the above they become maintainable because the code exists in the IDE and at the language level, rather than in tables in FitNesse.

If the implementation changes, the test still remains valid because it documents the capabilities and requirements and not the implementation.

I view FitNesse, and Cucumber, as a way of creating Domain Specific Languages to write executable specifications, which we can potentially also use for BDD.

I describe this in my BDD tutorial, but I’m not sure how well it comes across in the slides http://www.slideshare.net/eviltester/lessons-learned-with-bdd-a-tutorial

Hope that helps. Thanks for the Question.

Posted in FAQ, Selenium Simplified Blog | 2 Comments

Question: What else do I need to master?

“I have a question: What else do I need to master, besides Selenium and the obvious complementary stuff like HTML, CSS, JavaScript, JQuery?”

A question that opens a whole world of investigation. I guess different people will need to learn different things. So some of the ‘obvious’ complementary stuff first:

Basic Developer ‘stuff’:

  1. Learn your chosen programming language well
  2. Learn the Unit Test framework that you are going to use
  3. Learn the IDE you are going to use
  4. Learn the supporting tools e.g. maven
  5. Learn how to read the error messages of the tools and the programming language
  6. Learn how to use the debug features in your tools

Basic Web Stuff:

  1. As well as HTML and CSS, learn how the DOM hangs together and how to traverse it in the browser using the inbuilt developer tools and plugins like FireBug
  2. As well as JavaScript in general, learn the specific things that will likely make it tricky for you to automate – Ajax, DOM amendment, Event Handling
  3. Still useful to learn XPath for those parts applications that don’t have a good route through the HTML for locator strategies

Learn WebDriver:

  1. Get in the habit of reading the underlying WebDriver code. Especially the ‘Support’ classes as they offer good examples of how to ‘wrap’ or supplement WebDriver

Intermediate Developer ‘stuff’:

  1. Learn refactoring
  2. Learn how to build abstraction layers and various programming patterns
  3. Learn how to Unit test your supporting test code
  4. Learn Domain Driven Design to help you understand how to build domain specific abstraction layers

Intermediate Web ‘stuff’:

  1. Try and keep up to date with Web Development practices, even if it is just through reading news stand magazines on Web Development
  2. Setup your own web server and run apps so you learn how to read and access the server logs
  3. Learn to use Debug proxies so you can see the traffic and investigate the raw HTML as well as the DOM based HTML

Practice.

A lot of the skills I need for WebDriver have come from solving problems and building tests for real applications.

Try and automate applications that use a lot of Ajax, or a lot of 3rd party JavaScript libraries, or have auto generated code like the GWT apps.

Try and use other libraries, not associated with WebDriver, e.g. HTTP libraries, and see if you can use them in combination with WebDriver e.g. login with HTTP directly and move the cookies from the HTTP session into WebDriver, or from WebDriver across to an HTTP session.

Keep learning as much as possible. I can’t predict what you will need to know. But I guarantee that very little of the supporting technology that you can learn will ever turn out to have been something that wasn’t worth learning.

Posted in FAQ, Selenium Simplified Blog | Leave a comment

FAQ: What to do when things go wrong – first things first?

Things might go wrong when you follow online instructions, or tutorials you see on this blog or others. Your setup might not be the same as mine, you might not notice that some of the earlier steps have failed.

When things go wrong, drop down to the command line and make sure that the basics work:

  • mvn -v
  • javac -version

In your project folder make sure that “mvn clean” works without error.

Then try a “mvn compile” and make sure you don’t get any download errors reported.

Many of the recent questions I receive relate to simple maven configuration issues.

I have created 2 new videos, and a hints and tips lecture, which show some troubleshooting steps that you can take if you encounter maven problems.

These are over on the full Selenium WebDriver course, but I have made them free previews so you should be able to watch them even if you haven’t signed up for the full course.

  1. Maven Troubleshooting Hints
  2. Maven Troubleshooting Downloads
  3. Maven Troubleshooting Proxies and Cache issues

Please refer to these first, if you encounter any maven issues.

You can also read the slideshare presentation.

Posted in FAQ, Selenium Simplified Blog | Leave a comment

FAQ: What are my next steps after writing my first Selenium WebDriver test?

You have installed the tools? You have an IDE setup? You’ve written and run your first Selenium WebDriver test?

If not then follow our free tutorial videos on this very site, then come back here.

Web Resources

You need to:

  1. Browse the documentation on the Selenium HQ (http://seleniumhq.org/) site.
  2. Follow the official blog , particularly the “Smattering of Selenium” posts that Adam Goucher writes (http://seleniumhq.wordpress.com/)
  3. Read and follow my Selenium Simplified blog rss feed or
    subcribe to our email list

If you only do one of the above then make it step 2. Then follow the links in “Smattering of Selenium” to find other resources and links that are worth following.

Read Other People’s Code

When I learned to code I did so by reading other people’s code.

People tend not to do that today, so you can get an edge.

Remember the most important code to look at is the Selenium code base itself, so attach the source, and start browsing.

There are open source projects which use WebDriver – have a look at their code:

  1. RichFaces uses WebDriver in its testing https://github.com/richfaces
  2. The Vorburger project may have some interesting code to learn from https://github.com/vorburger/webdriver-reporting
  3. Thucydides is a library built on top of WebDriver which again you can read the code and see how other people have used WebDriver https://github.com/thucydides-webtests/thucydides

Feel free to do your own searches on koders.com  and grepcode.com

Learn Java

Recommended books for Java:

  • Java For Testers  by Alan Richardson
  • Effective Java by Joshua Bloch
  • Implementation Patterns by Kent Beck
  • Growing Object-Oriented Software, Guided by Tests by Steve Freeman and Nat Pryce

And probably in that order.

Experiment

The most important thing to do is to build automation code.

If you get stuck then consider our  full WebDriver Course. It goes into depth of the API with lots of exercises. It provides some structure as you learn, with working source code you can refer to if you get stuck.

But the most important thing is to start doing. Add automation where it adds value on your projects. Add Abstraction layers so that you don’t spend your entire time maintaining the code.

Just start experimenting.

Selenium 2 WebDriver API course

 

 

Posted in FAQ, Selenium Simplified Blog | 1 Comment

FAQ: Why Does Selenium Not Work With This Alert?

I received another question about Alerts this morning.

I receive this question frequently enough that I should consider it an FAQ.

So “Why does Selenium not work with my Alert?” or “Why can’t Selenium handle the alert in my application?”

There is an FAQ answer on the Selenium wiki that shows the Alerts API

But that typically doesn’t answer the question because the person asking the question has used the alerts API.

The answer I have given, in every single instance, takes the form “The thing you think is an alert, is not an alert”

The use of the web has changed. HTML has changed. We can now style div elements to look like dialogs and alerts, we can even give them focus and make them modal. That doesn’t mean they fit the definition of an Alert. i.e. something triggered by the JavaScript methods: alert, confirm or prompt

http://www.w3schools.com/js/js_popup.asp

The following are examples of alerts:

With any of the above examples, try and “inspect element” on the alert. You can’t. Because it is an alert.

The following examples are not alerts:

For each of the above, try and “inspect element” and you can. Because it is an HTML div, not an alert.

Live Examples of pseudo-alerts include:

  • those horrible “welcome to our website for the first time will you take our survey about our web site” dialogs
  • “you need to be logged in to see this content” overlays
  • “sign up for my mailing list and I’ll let you see what you came here for” overlay
  • “tweet this and then I’ll let you read it” overlay

Only use the Alert API on things that are Alerts. If you are using the Alert API, and it isn’t working, then the thing you are testing might not be an Alert.

Posted in FAQ, Selenium Simplified Blog | 3 Comments

FAQ: Why has my Firefox Selenium WebDriver Browser Not Closed

A few FAQs come up on the Start Selenium Simplified course. When they do, it usually means that I haven’t done a good enough job of making the answer visible the first time I address it.

One that crops up a lot is “Why is my browser not closing?”

Because WebDriver has two methods:

  •  .close
  • .quit

The documentation for .close says that it will close the browser if it is the last window that is closed.

For the last few Firefox releases, this does not happen.

Therefore we use .close, and .quit.

Use .quit when you know it is the last window you are working with. Or if you are working with a Driver Manager abstraction then you might want to stick the .quit in a JVM Shutdown.

I don’t know the reasons why, I haven’t spent the time investigating. But the workaround is to use .quit to finish with the driver.

 

Posted in FAQ, Firefox, Java, Selenium Simplified Blog, WebDriver | Leave a comment

FAQ: Why are my JUnit asserts showing as deprecated

I tend to use Hamcrest’s AssertThat now, but I still write Assert in some of my example code.

JUnit has a tendency to move code around, but it deprecates the old code, and there is usually a comment in the JUnit code somewhere explaining what to use instead.

But a common question on the Start Selenium WebDriver course relates to “Why are my asserts deprecated” and its usually because JUnit codebase has changed.

These changes are usually for the better, so keeping our code up to date and changing the import statements is a small price to pay.

Short Answer? Use the import from org.junit.Assert

 

Posted in FAQ, Java, Selenium Simplified Blog | Leave a comment