Selenium Simplified Notes 1 of X

So after checking out Alan Richardson’s free course over at udemy, I decided to check out his book.  I bought a digital copy of Selenium Simplified last month and started reading it. The downside of going with a book instead of his video trainings is I don’t get to hear his Irish accent. I try to read it with an accent (but only in my head) but it’s not really working out for me. Anyways, this is the part where I blog about my notes so that many months or years from now, in case I need to relearn what I’ve learned so far, I’ll just refer here instead of reading the entire book.

First things first – Setup

  1. Install Firefox (Although I highly suspect, Future Me will already have it installed.)
  2. Install Selenium IDE from Seleniumhq.org
  3. Install Java (Future Me, it might already be installed. Try java -version at the command line to check.)
  4. Install Selenium RC (well, it’s more of just copy the server .jar file and extract the client .zip files)
  5. Install Eclipse IDE for Java Developer
  6. When you create your Java project in Eclipse, you might need to configure your build path to add the server .jar and JUnit.

A simple java class

It’s not refactored. But it shows how to do a simple search and it has some simple assertions.

package com.eviltester.seleniumtutorials;

import com.thoughtworks.selenium.*;
import org.junit.Test;
import static org.junit.Assert.*;

public class MySimpleSearchTests {

@Test
public void searchAndCheckResults() {

Selenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.compendiumdev.co.uk/");
selenium.start();
selenium.open("/selenium/search.php");
selenium.type("xpath=//input[@name='q']", "Selenium-RC");
// When we use an XPath starting with '//', we can opt to leave out 'xpath='
selenium.click("xpath=//input[@name='btnG' and @type='submit']");
selenium.waitForPageToLoad("30000");

// Now here are the tests...
// Check if particular text is present
assertTrue(selenium.isTextPresent("seleniumhq.org"));
assertTrue("That long text is not found",
selenium.isTextPresent(
"Selenium Remote-Control Selenium RC comes in two parts." +
" A server which automatically launches and kills browsers,"
));

// Check if particular URLs are found
int matchingCountTotal = 0;
matchingCountTotal += selenium.getXpathCount("//a[@href='http://selenium-rc.seleniumhq.org']").intValue();
matchingCountTotal += selenium.getXpathCount("//a[@href='http://selenium-rc.seleniumhq.org/']").intValue();
// use starts-with instead
matchingCountTotal += selenium.getXpathCount("//a[starts-with(@href, 'http://selenium-rc.seleniumhq.org')]").intValue();
assertTrue("None of those URL were found", matchingCountTotal>0);

// Check if page title contains a particular text
String pageTitle = selenium.getTitle();
assertTrue("Search term not found in page title: " + pageTitle,
pageTitle.contains("Selenium-RC"));

// Check if the search box contains the search term
String searchTerm = selenium.getValue("xpath=//input[@name='q' and @title='Search']");
assertTrue("Search term not found in search box, contains this instead: " + searchTerm,
searchTerm.equals("Selenium-RC"));

selenium.close();
selenium.stop();

}

}

Start and stop the Selenium server

Don’t forget to start this up before running your test. Run this at the command line: java -jar selenium-server-standalone-2.33.0.jar To stop, just CTRL+C while at the command window. Or use this at your browser: http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer

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