Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a Selenium test in Java

So I used Selenium IDE to create a test case for some automation I want done. I want to be able to create some looping/flow control for this case so I figured I would need to export it out of Selenium IDE to something like Java (I'm most familiar with Java). I exported to Java/JUnit4/Web Driver. I think trying to execute the java file through Eclipse would work best, although if someone knows something easier, let me know. Anyway, I have found NO GOOD EXPLANATION on how to execute this Java through Eclipse.

Most things I read tell me to make sure my Build Path libraries includes the Selenium Standalone Server. Virtually all things I read tell me to use the Selenium Remote Control. However, I thought the RC was depreciated, and I am wondering if there is anyway to make it work with the more recent Web Driver stuff I downloaded from Selenium. Also, most things I read tell me I need to use public static void main(), which is a little awkward because I don't know how to alter the code the exported selenium gives me (obviously I can't just paste it all in the main method).

If anyone could walk me through from exportation of Selenium to Java to executing the code, I will be forever in your debt.

The code Selenium gives me: package com.example.tests;

package com.rackspace;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class RackspaceContactAutomation {
   private WebDriver driver;
   private String baseUrl;
   private boolean acceptNextAlert = true;
   private StringBuffer verificationErrors = new StringBuffer();

   @Before
   public void setUp() throws Exception {
      driver = new FirefoxDriver();
      baseUrl = "https://cp.rackspace.com/Exchange/Mail/Contacts/List.aspx?selectedDomain=blahblahblah.com";
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
   }

   @Test
   public void testContactAutomationJava() throws Exception {
      driver.get(baseUrl + "/Exchange/Mail/Contacts/List.aspx?selectedDomain=blahblahblah.com");
      driver.findElement(By.linkText("Mr. Man")).click();
      driver.findElement(By.linkText("Contact Information")).click();
      new Select(driver.findElement(By.id("PhoneNumberType"))).selectByVisibleText("Mobile");
      driver.findElement(By.id("MobilePhone")).sendKeys("999-999-9999");
      new Select(driver.findElement(By.id("PhoneNumberType"))).selectByVisibleText("Fax");
      driver.findElement(By.id("Fax")).sendKeys("999-999-9999");
      driver.findElement(By.cssSelector("button.primary")).click();
   }

   @After
   public void tearDown() throws Exception {
      driver.quit();
      String verificationErrorString = verificationErrors.toString();
      if (!"".equals(verificationErrorString)) {
         fail(verificationErrorString);
      }
   }

   private boolean isElementPresent(By by) {
      try {
         driver.findElement(by);
         return true;
      } catch (NoSuchElementException e) {
         return false;
      }
   }

   private boolean isAlertPresent() {
      try {
         driver.switchTo().alert();
         return true;
      } catch (NoAlertPresentException e) {
         return false;
      }
   }

   private String closeAlertAndGetItsText() {
      try {
         Alert alert = driver.switchTo().alert();
         String alertText = alert.getText();
         if (acceptNextAlert) {
            alert.accept();
         } else {
            alert.dismiss();
         }
         return alertText;
      } finally {
         acceptNextAlert = true;
      }
   }
}

This gives me 4 errors (3 for the annotations, which I could just delete, and one for fail in the tearDown() method. It's not the errors I'm concerned about so much the how do I make this code actually execute?

Thanks!

like image 475
Man Friday Avatar asked Aug 28 '13 15:08

Man Friday


1 Answers

A good way to run Selenium Java code in Eclipse is to run them as JUnit tests.

1. Create a Maven Project in your Eclipse.
If you haven't done this before, see:

  • How to install/integrate Maven with Eclipse
  • How to create a simple Maven project

2. Add the following dependencies to your pom.xml file:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.7</version>
    <scope>test</scope>
</dependency>    
<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.25.0</version>           
</dependency>    
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-firefox-driver</artifactId>
    <version>2.33.0</version>
</dependency> 
<dependency><groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>2.25.0</version>    
</dependency>

3. Copy your exported Java file into the Maven Project.

4. Add the following imports to the file:

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

5. Run the Java file as a JUnit test, like so:

Example from my Eclipse (Version is Kepler)

like image 51
James Dunn Avatar answered Oct 15 '22 06:10

James Dunn