Mastering Selenium Testing Tools

This entry provides many links and step-by-steps to get Selenium working for you! However, I never get it working myself. And before the end of the entry, the project I’m ramping up for gets shelved and I’m sent back to Salesforce.

1 Setting UP The Work Environment

Here’s some books on this subject: Selenium WebDriver Practical Guide, and Mastering Selenium WebDriver.

1.1 Preparing Your Environment

Selenium IDE: record journey through website on Firefox, save it, export to various languages, assert presence / abundance of items in page. Great to get you off the ground.

Selenium WebDriver: allows greater flexibility, which would be done via a programming languages. Done using WebDriver (execute locally) and Selenium Server (execute remotely). Can use Ruby, of course, but also JavaScript—as well as a bunch that I don’t know. And I have to know enough to do a Hello World.

1.1.1 CodeCademy – Learn Java

1.1.1.1 COnditionals And Control Flow

Precedence in evaluating Boolean operators:

! is evaluated first.
&& is evaluated second.
|| is evaluated third.

1.1.1.2 Object Oriented Java

There’s a thing called the main method. It executes when Java runs the program.

    public static void main(String[] args) {

    }

Look at this method:

  public void bark(){
    System.out.println("Woof!");
  }

The void bit means that the function returns nothing. If instead of void it said int or another type of variable, that would mean that the function would return an integer (in the case of int). So:

public int numberOfTires() { 
  return 4; 
}

1.2 Preparing Your Environment

Now I know the whole Java.

Other resources include: Java Book: The Complete Reference by Herbert Schildt, TutorialsPoint.com, and Learn Java Online.

Also, we need a Verification Framework—which means nothing to me. So that could be:

  • Junit (Java)
  • TestNG (Java)
  • Spock (Java)
  • Rspec in Ruby
  • Nose in Python
  • PHP
  • Nunit and MSTest for C#

This course is going to use Junit. Here’s a bunch of sites it spits out:
Junit.org
Maven Repository
Maven.
– And also there’s this [site about Maven at TutorialsPoint

Also we need Firebug and FirePath.

OKAY just to rehash. I’m going to try using Ruby since I already have experience with it. Which means I need to download the following ingredients:

On my first attempts, I can’t get Selenium IDE to work properly in Firefox—that is, the context menu does not appear.

  • I’m using Selenium IDE 2.9.1
  • I tried in Firefox Developer Edition 53, it didn’t work.
  • I tried in Firefox 51, it didn’t work.
  • On OSX, Java is up to date.
  • I don’t know how to open the .jar file I downloaded for Selenium Server.
    • Open terminal
    • cd into my downloads directory
    • java says I need to download JDK
    • java -jar /path/to/your/file.jar
    • Says the port’s already being used—which I thought maybe due to running the .jar from Finder, but it’s not showing up in Activity Monitor.
    • Downloading Java JDE
  • I posted in the Google Groups – Selenium Users forum about six hours ago, and since it’s my first post it hasn’t been approved yet.
  • I posted in StackOverflow, which nobody responded to.
  • I installed Firefox ESR (Extended something Release), which is meant for large organizations who are generally fearful of installing the latest version of anything. So, in this case, it’s Firefox of 6 versions back.

So, let’s go back through what we have installed for getting started:
– Selenium IDE
– Firefox ESR
Firebug, which isn’t being updated, but I don’t have any other choice since I’m now using an old browser.
Firepath

I’m beginning to think that maybe I should just use the Java stuff they talk about since everyone else is doing it. Anyway, onward.

2 Selenium IDE

As an example:

  • Open Selenium IDE
    • By default it should be recording
  • Go to a page
    • Follow a link on it
    • Right click on the page, and select Assert Title
  • Go back to Selenium

Now you’ll see a list of all the things you just did, and Assert Title–essentially what that means is “I’m saying that the title is X” – and at the top of the IDE, if you click the play, it’ll repeat what you just did, except automated, and when it gets to that line, if the title is X, then the result is that the test passes. If you edit the list so that it says to assert the title is Y, and run it again—it’ll fail, because the page is still X, but you said you wanted it to be Y. Okay, that makes sense.

But just as fast as they give us something easy to use, they take it away, saying that it’s hard to reuse, can only do one path through a script, no repetition, and only Firefox.

In the meantime, assert stops the script from running, while verify notes it, and continues running the script.

You can export the test case as Java. Or Ruby. But I feel like I should be doing Java so I can be like everyone else.

Two or more test cases can be combined into a test suite. It’s not as flexible as using methods, but it can be seen as a way to reuse functionality. Sounds like it sucks. You’d have multiple test cases open, and save them as a test suite. You can then use the other green play button to play the entire test suite.

The tutorial ends by teaching us how to uninstall it. A sad ending for something I spent six hours trying to install in the first place.

3 Selenium Webdriver

3.1 Introducing Selenium Webdriver

WebDriver lets us program Selenium using our favorite programming languages.

Now we’re instructed to go download the Java JDK and test it by opening terminal and typing java –version. Also you need a compiler. You can type javac –version and it’ll tell you its version.

Now it’s time to set the Path. Unfortunately, Lynda neglects to offer instructions for OSX, so we’re on our own.

3.1.1 Adding Java to Path

In terminal you can open the file everybody’s talking about by using Nano, which is easier to use than Vim because it at least doesn’t make you google for how to do every little thing. Type:

nano ~/.bash_profile

and it’ll open that up. For me, it’s a blank document. I don’t know if that’s correct or not. You can go ahead and close it with Ctrl-X.

Now, rather than entering stuff right into nano, try this in the terminal:

echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile

Now, return to nano and open back up that bash_profile like you did before. Here’s a valuable lesson: just because you find something on the internet doesn’t mean you should type it into your terminal. In this case, what you just typed was wrong. How do I know? I don’t. But I do know I can’t see hidden files on my computer, so I can’t get my hands on half the stuff I’d like to check out.

3.1.1.1 Unhide Hidden Files on OSX

Here’s how to unhide hidden files and folders in OSX—in this case, El Capitan. For more detail, you can read this article, but I’ve pretty much condensed it to two steps:

  1. In Terminal, defaults write com.apple.finder AppleShowAllFiles YES
  2. Relaunch Finder by Alt-Right-Clicking on it.

3.1.2 Adding Java to Path, Continued

Let’s try to find the path of Java, or something like that. Oh! Who knew, the path was maybe correct—which we can now confirm since we’ve unhidden files.

At the root AKA Macintosh HD you can actually file the path usr > libexec > java_home, which turns out to be a shortcut:

This is the natively supported way to find out both the path to the default Java installation as well as all alternative ones present.

But there is an important difference here: the version is incorrect. We can specify the version so that if we have multiple versions of Java installed, we can target the one we want. The path up there is for 1.7, but I have 1.8 installed. How do I know?

System Preferences > Java > Java > View > Platform = 1.8

So, let’s return to Nano and edit and save it.

3.1.2.1 Noah helps

Noah has provided me with advice. He suggests to add this instead:

export JAVA_HOME=$(/usr/libexec/java_home)

which is the same as the above, but without the version.

And secondly, install Homebrew via Terminal with this command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Basically, it will keep your packages in one spot, and make a symbolic link to them from /usr/local/bin. So you will just have to set your PATH to start with /usr/local/bin, and you will have access to all of the packages you have installed through it. pretty easy-pz. And to set path for terminal on every session, you will need to put a PATH export command in your ~/.bash_profile (or ~/.bashrc, or ~/.profile, they all do the same thing). all of the above load up on terminal start and will set your environment variables for ya

And lastly, he suggests:

doing a fresh install of java via homeberw (because you will start to keep all of your packages there anyways, why not java too)

So, copying the stuff from Noah’s .bash_profile and removing things related to programs I don’t have:

PATH="/usr/local/bin"
PATH="$PATH:/usr/bin"
PATH="$PATH:/bin"
PATH="$PATH:/usr/sbin"
PATH="$PATH:/sbin"
PATH="$PATH:/usr/local/bin"
PATH="$PATH:/usr/local"
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH

and to install Java via Homebrew:

brew update
brew cask install java

Conversation:

[10:56]  Noah: sweet

[10:57]  
now, type this

[10:57]  
`brew doctor`

[10:57]  
and then

[10:57]  
`brew upgrade`

[10:57]  
`brew update`

[10:57]  
Stephen Frost whoa

[10:57]  Noah: `doctor` diagnoses if there are any system weird things going on that you should fix, if nothing then ur good

[10:57]  
and `upgrade` checks if all of your brew apps are newest version

[10:58]  
and `update` makes sure homebrew is actually the newest homeberw

[10:58]  
Stephen Frost i've got tons of unexpected header files

[10:58]  Noah: okay. it may or may not be an issue

[10:58]  
Stephen Frost hahaha

[10:58]  Noah: basically, if everything is working as you need it to, ignore what it is saying

[10:58]  
Stephen Frost they're all related to node

[10:58]  
okay

[10:58]  Noah: but if something is broken and it says something, maybe try to fix it 

3.1.3 Installing Maven

Next, we’re supposed to add Maven as the build tool. Installing it sounds like a hassle, but now that I have Homebrew, maybe we can use that instead. Yeah we can!

brew install maven

And we can test its installation like so:

mvn –version

The tutorial says to add it to the PATH, which, again, it gives no instructions for OSX. I found these instructions on StackOverflow. Figure out where the Maven bin directory is. When you do the mvn –version it’ll show a location, and I when to Finder’s Go To Folder and typed that in and looked around until I saw the bin, right-clicked for more info, and copied the path from there. Which, in my case was /usr/local/Cellar/maven/3.3.9. And then go back to

nano ~/.bash_profile
PATH="$PATH:/usr/local/Cellar/maven/3.3.9"

3.2 Implementing Our First Example

Our goal in this chapter is to perform the same actions as we did using Selenium IDE, but this time by scripting it. In short:

  1. Go to http://www.seleniumhq.org
  2. Find the WebDriver documentation and click on the link
  3. Verify the page title.

The tutorial briefly launches into a plug for IntelliJ IDE, which confuses me because I’m not sure if I need it, or if the IDEs I already have installed are okay to use. Also, I need to download some prepared files, so I can’t write them myself. Here’s the file we’re looking at. Yikes:

package packt.selenium.chap3_2;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import static org.junit.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

/**
 * Created by Ripon on 11/4/2015.
 */
public class WebDriverDocumentationTest_01 {

    public static void main(String args[]){
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        String baseUrl = "http://www.seleniumhq.org";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.get(baseUrl + "/");
        driver.findElement(By.linkText("Documentation")).click();
        driver.findElement(By.linkText("Selenium WebDriver")).click();
        assertEquals("Selenium WebDriver — Selenium Documentation", driver.getTitle());
        driver.quit();
    }
}

The section of this code we’re looking at now is the meaty part of it, the part that makes sense to me. It’s easy to read.

        driver.get(baseUrl + "/");
        driver.findElement(By.linkText("Documentation")).click();
        driver.findElement(By.linkText("Selenium WebDriver")).click();
        assertEquals("Selenium WebDriver — Selenium Documentation", driver.getTitle());
        driver.quit();

I love how these tutorials are like, “Step 1. Install some stuff. Step 2. Refactor!” The issue here is that we’re looking at navigation, finding elements, and title assertion. All in one little section. This is how I write code!

This is how they refactor it:

package packt.selenium.chap3_2;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import static org.junit.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

/**
 * Created by Ripon on 11/4/2015.
 */
public class WebDriverDocumentationTest_02 {
    private static WebDriver driver;
    private static String baseUrl;

    public static void main(String args[]){
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        baseUrl = "http://www.seleniumhq.org";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        testVerifyTitleWebDriver();
        driver.quit();
    }

    //navigation
    public static void navigateToSeleniumWebDriver() {
        driver.get(baseUrl + "/");
        driver.findElement(By.linkText("Documentation")).click();
        driver.findElement(By.linkText("Selenium WebDriver")).click();
    }

    //Validation
    public static void testVerifyTitleWebDriver() {
        navigateToSeleniumWebDriver();
        String actualTitle = driver.getTitle();
        assertEquals("Selenium WebDriver — Selenium Documentation", driver.getTitle());
    }
}

So, this shows a navigation function, and a validation function. Oh, but there’s so much more…that we also had nothing to do with. New classes SeleniumHQ.java, Documentation.java, WebDriverDocumentation.java, and then the current file. WHAT?!?!

There’s a principle that states that each thing should perform only one type of function, i.e., navigate, or assert—not both. So Documentation.java does nothing but locate and click on the Documentation link.

So he refactors further by creating that Documentation.java file and refactoring the code to:

package packt.selenium.chap3_2;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import static org.junit.Assert.assertEquals;

/**
 * Created by Ripon on 11/4/2015.
 */
public class WebDriverDocumentationTest_03 {

    public static void main(String... args){
        WebDriver driver = new FirefoxDriver();

        SeleniumHQ seleniumHQ = new SeleniumHQ(driver);
        Documentation documentationPage = seleniumHQ.clickDocumentation();
        WebDriverDocumentation webDriverDocumentation = documentationPage.navigateToWebDriverDocumentation();

        String actualTitle = driver.getTitle();
        String expectedTitle = "Selenium WebDriver — Selenium Documentation";
        assertEquals(expectedTitle, actualTitle);
        driver.quit();
    }
}

See, the difference here is that we’re not doing the clicking on this page. Great. YOU’RE A HORRIBLE TEACHER.

Next, he mentions that we could end up with orphaned browser windows if we don’t shut them. I’ve experienced this. If you don’t want the responsibility of closing them manually, you need to program it to close the windows for you, regardless of success or failure.

Also, if you put a suffix Test in your filename, Maven will execute them, in this case: WebDriverDocumentation_04Test.java

So we cd into the directory that includes the pom.xml file, and run maven clean test on it. At which point it works in the tutorial, but for us it just opens up a Firefox window and sits there. I tried deleting the newest version of Firefox, but even the old one doesn’t work. Fortunately, if I wait long enough, I get some errors!

Tests in error: 
  testVerifyTitle_WebDriverDocumentation(packt.selenium.chap3_2.WebDriverDocumentation_04Test): Failed to connect to binary FirefoxBinary(/Applications/Firefox.app/Contents/MacOS/firefox-bin) on port 7055; process output follows: (..)
  testVerifyTitle_WebDriverDocumentation(packt.selenium.chap3_2.WebDriverDocumentation_04Test)

Tests run: 2, Failures: 0, Errors: 2, Skipped: 0

Time to begin troubleshooting.

No help because I’m not having an issue with Firefox trying to update itself. Possible that it’s not finding Firefox.app, so I delete my Firefox installations and re-install using Homebrew, which according to this is supposed to create a symlink that can be followed automatically by whatever else I’ve installed via Homebrew.

Still no difference.

Next, this page suggests that the problem is that Maven is configured for a different version of Firefox than the one I’m running. This is possible since the pom.xml came from Lynda.

I’ve tried downgrading to Firefox versions that supposedly work based on this answer from StackOverflow:

Scenarios :
1.  As per Selenium Webdriver 3.0 Documentation and Geckodriver Configuration Document,
Selenium 3.0 and higher version require Java8 and Firefox 46,47+ versions.
Note : Here you require to use geckodriver
2.  For Selenium Webdriver 2.53.1 and Lesser version,
Require lesser version from Firefox 45 and Java7
Note : Here only WebDriver driver = new FirefoxDriver(); will work perfectly.
Please configure system as per above combination and verify your script.
As per given comments, Answer is scenario 2.

But I can’t figure out how to downgrade Java to 7. But, according to this other StackOverflow advice:

This was happening because of new issue in latest browser of firefox.
Update your selenium jars. The new version of firefox(Or different browser) is not supporting old jars of selenium.
Download both Selenium Server (formerly the Selenium RC Server) Selenium Client & WebDriver Language Bindings
Replace them with old jars you are using. Update your mozilla also so you can get the updated results
source :- http://docs.seleniumhq.org/download/
To overcome from this issue you also need to setPreference as xpinstall.signatures.required", false to firefox Profile and then pass it to driver object
firefoxProfile.setPreference("xpinstall.signatures.required", false);
Below code is working fine for me.
static WebDriver driver=null;
public static void main(String[] args) {
final FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("xpinstall.signatures.required", false);
driver = new FirefoxDriver(firefoxProfile);
driver.get("https://www.google.de/");
Hope it will help you 🙂

If I can figure out how to do something with the jars I’ll be okay. Whatever the hell that means.

3.2.1 OMG SOMETHING MUST WORK

Based on this article, I’m going to go step by step and maybe this will be key. What makes it different is that it involves doing things with the jars.

  1. Download and install the Java JDK. I’ve already done this.
  2. Download and install Eclipse IDE. All they currently have that sounds like what I want is Eclipse Neon. When I go to install it, there’s a long list of possible installations, so I go with the top one, for Java developers.
  3. Download the Selenium Client & WebDriver Language bindings that are Java-specific. You’ll end up with a whole bunch of jar files
  4. Open Eclipse
    a. It asks for a Workspace, and I’m just selecting the default
    b. Clicking create New java project; Named MyProject (and then click through till finished)
    c. New > Package, named myPackage
    d. Right click on myPackage, select new class, MyClass
  5. Add Jar files to library
    a. Right click on MyProject > Build Path > Configure Build Path
    b. Libraries tab > Add External Jars
    c. Select all those jars you downloaded in Step 3 above, add them (both the ones in the lib folder and the one directly outside it)

Additional (ridiculous) steps:

  1. Make sure you have the latest version of Firefox…again.
  2. Download the latest geckodriver
  3. See this line? WebDriver driver = new FirefoxDriver(); Well right before it add this line: System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");

Figured out using, WebDriver cannot be resolved to a type, and how to use the gecko executable with selenium

  1. Guess what? The project just fell through because I’m not a .NET developer. See ya!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s