Selenium 2 Java Quick Setup
I had a seemingly random issue with a website where it would fail one out of x times. Since I haven't used Selenium 2 yet I thought I would run a Selenium test.
To get started in Selenium 2 and Java the quickest way is to use Selenium IDE and generate some tests. Then use the File -> Export Test Case As -> Java / JUnit 4 WebDriver Backend. After the test is created then just drop it into Netbeans or Eclipse. The first stumbling block I ran into was that I needed Both Selenium Client and Selenium Server Jars added. Without the Selenium Server Jar, I saw an unhelpful error that stated: "com/google/common/base/Function". After adding those 2 Jars, if you don't use the Eclipse or Netbeans test runners, make sure to add the JUnit Jar. Then you can run your test. Here is a shell that does nothing but run a test that was outputted from Selenium IDE.
To get started in Selenium 2 and Java the quickest way is to use Selenium IDE and generate some tests. Then use the File -> Export Test Case As -> Java / JUnit 4 WebDriver Backend. After the test is created then just drop it into Netbeans or Eclipse. The first stumbling block I ran into was that I needed Both Selenium Client and Selenium Server Jars added. Without the Selenium Server Jar, I saw an unhelpful error that stated: "com/google/common/base/Function". After adding those 2 Jars, if you don't use the Eclipse or Netbeans test runners, make sure to add the JUnit Jar. Then you can run your test. Here is a shell that does nothing but run a test that was outputted from Selenium IDE.
package webdriversample; import com.thoughtworks.selenium.Selenium; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriverBackedSelenium; import org.openqa.selenium.firefox.FirefoxDriver; public class WebDriverExample { private Selenium selenium; @Before public void setUp() { WebDriver driver = new FirefoxDriver(); String baseUrl = "http://google.com"; selenium = new WebDriverBackedSelenium(driver, baseUrl); } @Test public void googleSearchExample() { selenium.open("?q=test"); Assert.assertTrue(selenium.isTextPresent("Google")); } @After public void tearDown() throws Exception { selenium.stop(); } public static void main(String[] args) { Result result = JUnitCore.runClasses(WebDriverExample.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } } }
Comments
Post a Comment