2

My end goal is to confirm that I have Java, Selenium WebDriver, and JUnit functioning such that I can run an automated test case utilizing these technologies.

What I have done so far:

  1. installed Selenium IDE (v 2.8.0) for Firefox (v 34.0.5)
  2. recorded a simple test case using Selenium IDE and added the test case to a test suite
  3. in Selenium IDE exported the test case using the option: File->Export Test Case As...->Java / JUnit 4 / WebDriver
  4. downloaded and installed JUnit 4 from: https://github.com/junit-team/junit/wiki/Download-and-Install (Plain-old jar method)
  5. downloaded and installed Selenium client and WebDriver Java language binding (v 2.44.0) from: http://www.seleniumhq.org/download/
  6. performed some slight modifications to the code generated in step 3 such as removing the package statement at the beginning
  7. compiled the code with the following command (windows command prompt):

C:\docs\tech\code\myCode\java\testing\selenium\utest\practice>C:\java\jdk\bin\javac.exe -cp C:\docs\installs\programming\automation\test\xUnit\java\jUnit\junit\v4_12\junit-4.12.jar;C:\docs\installs\programming\automation\test\xUnit\java\jUnit\hamcrest\hamcrest-core-1.3.jar;C:\docs\installs\programming\automation\web\selenuim\webdriver\java\selenium-2.44.0\selenium-java-2.44.0.jar C:\docs\tech\code\myCode\java\testing\selenium\utest\practice\TestGooglePlayApp.java

this compiles with no warnings/errors

  1. attempt to execute the code with the following command:

C:\docs\tech\code\myCode\java\testing\selenium\utest\practice>java -cp .;C:\docs\installs\programming\automation\test\xUnit\java\jUnit\junit\v4_12\junit-4.12.jar;C:\docs\installs\programming\automation\test\xUnit\java\jUnit\hamcrest\hamcrest-core-1.3.jar;C:\docs\installs\programming\automation\web\selenuim\webdriver\java\selenium-2.44.0\selenium-java-2.44.0.jar TestGooglePlayApp

Actual Result:

The output of this attempt is:

Exception in thread "main" java.lang.NoSuchMethodError: main

No other error/warning is given and no indication is given that the code begins to execute

Expected Result:

This is my first time interacting with these technologies so I'm learning as I go! However, based on the research I have done my expectation is that a local Selenium WebDriver instance will be initialized as a Firefox Driver. This Firefox Driver will be sent instructions by the Java code in order to carry out the steps in the test case. JUnit will then indicate somehow on the command line the Pass/Fail status of the test case.

The big assumption I have here is that the Selenium Server/Selenium RC is not required in this case since all I intend to do is execute a local Selenium WebDriver script. Furthermore, my hope is that this can be launched directly from the command-line independent of Eclipse, Maven, etc. I would like to be able to send the final Java class to a colleague and the only dependency for expected execution would be a working SDK of Java on the end machine.

What's Next?

I'm looking for advice on what I can do to get this code up and running under the restraints I have outlined. It may be that the code needs to be modified. It may be that my expectations need to be tempered down and it's just not possible to do everything I want directly from the command-line. It may be that I did not compile the code correctly or some library is missing. It may be that... okay you get the point!

Other Relevant Details:

OS = Windows 7 - 64 bit

Here is the contents of TestGoogleApp.java:

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class TestGooglePlayApp {
  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://www.google.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void test000() throws Exception {
    driver.get("http://www.google.com/");

// COMMENT: Assert the 'Apps' icon is present and then click on it
assertTrue(isElementPresent(By.cssSelector("a.gb_C.gb_Sa")));
driver.findElement(By.cssSelector("a.gb_C.gb_Sa")).click();

// COMMENT: Assert the 'Play' app is present and then click on the 'Play' app and wait for the expected contents to load
assertTrue(isElementPresent(By.cssSelector("#gb78 > span.gb_s")));
driver.findElement(By.cssSelector("#gb78 > span.gb_s")).click();

// COMMENT: Assert that the input element associated with search queries is present and then type 'Testing' in the input element associated with search queries
assertTrue(isElementPresent(By.id("gbqfq")));
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("Testing");

// COMMENT: Click on the 'Search' element to perform a query for the input query term previously entered and wait for the expected contents to load
driver.findElement(By.id("gbqfb")).click();

// COMMENT: Assert that the desired result is contained in the search results returned and then click on the desired search result and wait for the expected contents to load
for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.linkText("Software Testing Concepts"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
}

driver.findElement(By.linkText("Software Testing Concepts")).click();
for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if ("Software Testing Concepts - Android Apps on Google Play".equals(driver.getTitle())) break; } catch (Exception e) {}
    Thread.sleep(1000);
}


// COMMENT: Assert that the page contains the expected content as follows: 1) title of app   2) user rating   3) number of users who have rated the app   4) format of text value of user rating (4.0)   5) format of text value for number of user who have rated the app (128)
for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.cssSelector("div.score"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
}

for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.cssSelector("span.reviews-num"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
}

try {
  assertTrue(Pattern.compile("[0-9]\\.[0-9]").matcher(driver.findElement(By.cssSelector("div.score")).getText()).find());
} catch (Error e) {
  verificationErrors.append(e.toString());
}
try {
  assertTrue(Pattern.compile("[0-9]+").matcher(driver.findElement(By.cssSelector("span.reviews-num")).getText()).find());
} catch (Error e) {
  verificationErrors.append(e.toString());
}

// COMMENT: store value for user rating and store value for number of users who have rated the app
String _currentUserRating = driver.findElement(By.cssSelector("div.score")).getText();
System.out.println(_currentUserRating);
String _numOfUserRatings = driver.findElement(By.cssSelector("span.reviews-num")).getText();
System.out.println(_numOfUserRatings + "HELLO");
  }

  @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;
    }
  }
}
autostack
  • 21
  • 2
  • Already answered here: http://stackoverflow.com/q/2235276/3124333 – SiKing Dec 18 '14 at 22:08
  • Thanks. This led me to the correct solution. Although as a newbie, it would have saved me some time knowing that in the context of: java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name] that [test class name] should be interpreted as so for me NAME_OF_TEST_CLASS = TestGooglePlayApp – autostack Dec 24 '14 at 09:59
  • You can post an answer to your own question. See: https://stackoverflow.com/help/self-answer – SiKing Dec 24 '14 at 15:23

0 Answers0