Note: PRACTICUM is a continuing series in what I refer to as a somewhat “Live Blog” format. Sections may vary in time to complete. Some may go fast. Some may take much more time to get through. Updates will be daily, they may be more frequent. Feel free to click refresh to get the latest version at any given time. If you see “End of Section” at the bottom of the post, you will know that this entry is finished :).
Chapter 6: Working With WebDriver
The whole point of running Selenium is so that we can interact with and work with different browsers and tet to make sure that interactions behave as they should, items appear where they should, and that our applications work in the environments that we want them to. Selenium WebDriver is designed to work with the major browsers currently on the market, so this chapter will show us how to interact with Chrome, Firefox, Internet Explorer and Opera. Each browser requires an executable be installed on the system, as well as the browser in question (the Firefox driver is already bundled with the Java client; it’s what we’ve been using thus far in the examples):
- IE Driver Executable: http://code.google.com/p/selenium/downloads/list
- Chrome Driver Executable: http://code.google.com/p/chromium/downloads/list
- Opera Driver Executable: https://github.com/operasoftware/operadriver/downloads
So far, this is the approach we have been using up to this point. The example below shows a standard JUnit test:
This example shows the bare basics of a test with no modifications.
Firefox Profile Preferences
In many cases, there are functions in the browser that a user may or may not want to have configured while they run their tests. To take advantage of this, a variety of profiles can be created, and the particular profile instance can be loaded and used. To do this, we:
- instantiate a Firefox Profile object and then update the settings.
- pass this object into FirefoxDriver where we instantiate it.
- load the profile with your details you have set.
Imagine that you wanted to have the book site be the startup page for Firefox. Below shows us how we can do that:
We can also point to a plug in using the following syntax:
Alas, this too fails for us, and for the same reason. In fact, many of the items in this chapter have the same “fatal flaw”. I think they look like great ideas, but why oh why can’t I compile them?!
Working with ChromeDriver
The next example shows us how we can load the ChromeDriver, and thus connect with Chrome. For this purpose, I took a copy of ChromeDriver and put it in /usr/bin so I could always rely on its position. With that, we compiled and ran, and lo and behold, here we are:
There’s a fair amount of red down at the bottom, but don’t be alarmed, that’s the output from ChromeDriver. The thing to focus on is that ChromeDriver opens, and the test runs, and passes. Woot!!!
However, it looks like ChromeDriver and the extended examples suffers from the same issues as FirefoxDriver. Try as I might, this won’t compile 🙁 (this is the example to explicitly call out the file location. IT’s complaining that it expects a string, not a file. Remove the File declaration, and it does the same thing. I’m at a loss. Java peoples, please tell me, it it me, or is something missing from this example?):
Well, OK,; not completely golden. Those astute viewers will notice I did a little cheat here. I set the assert value to be zero. This means it will return a true even if the element isn’t there. Why would I do such a thing? I wanted to show a completed test. Alas, the element I’m looking for stubbornly refuses to respond (it may be my system settings, I’ll tweak with them some more later). Just to show you the full story, set the asset value to equal one, and here’s what I get, at least as of right now:
- Download Intellij IDEA and install it
- Install Java SDK
- Download Selenium-Server-Standalone-x-xx.jar
- Download JUnit.jar
- Download IEDriverServer.exe
- Link up the IDE to find the Java SDK folder, plus add selenium-server-standalone and JUnit jars as global libraries
- Put the IEDriverServer in a place where the PATH variable can find it (or add it to your path).
OK, wow, that was pretty cool. That’s four different browsers we can drive and little in the way of tweaking top get them to do their things. I must admit, I’m a bit frustrated about the File and Dictionary calls, as well as the compilation errors. I followed all of the steps as requested but alas, many of these examples were for naught. I really would like to understand why they are not doing what they are supposed to do, and if there’s a way to make these work as printed. Again, is this because of me using Darwin and not Linux? Is it something special about Java 7? Do the examples rely on something that is now deprecated? Either way, we were able to set something running for all four browsers, and the fact I could get it to work on Windows has me somewhat elated.
End of Section.