How to run a Selenium test in Java

So I used the Selenium IDE to create a test case for some automation that I want to do. I want to be able to create some kind of loop / flow control for this case, so I decided that I would need to export it from the Selenium IDE to something like Java (I'm most familiar with Java). I exported to Java / JUnit4 / Web Driver . I think that trying to execute a java file through Eclipse will work best, although if someone knows something easier let me know. Anyway, I havenโ€™t found NO GOOD DEVELOPMENT on how to run this Java through Eclipse.

In most of the cases I read, tell me to make sure my Path Path libraries contain a standalone Selenium server . Pretty much everything I read tells me to use Selenium Remote Control. However, I thought RC was worthless, and I wonder if there is a way to make it work with the newer web driver materials that I downloaded from Selenium. Also, most of the things I read tell me that I need to use public static void main () , which is a bit inconvenient because I donโ€™t know how to change the code that exported selenium gives me (obviously, I can't just paste all this into the main method).

If anyone could get me through exporting Selenium to Java to execute code, I will be forever in your debt.

Selenium code 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 annotations, which I could just delete, and one for fail in the tearDown() method. These are not the errors I'm worried about how to do this, is the code really executing?

Thanks!

+7
java eclipse selenium
source share
3 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 yet, see:

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 the exported Java file to 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, for example:

Example from my Eclipse (Version is Kepler)

+14
source share

The previous answer is all legal. But to run directly from your eclipse, you need to make some changes to your code. To run the unit code, you do not need an open void main. So, here are the steps that allow you to simply copy the code and paste it into eclipse and run as a JUnit test:

  • Install JUnit in eclipse-> Help-> eclipse marketplace-> find JUnit and install it, restart eclipse.

  • Create a project in eclipse and a new package, and then create a new class with the same name as your exported Selenium IDE code, delete everything except the package line.

  • copy and paste the code from the Selenium IDE into this class, delete the package line.

  • Right-click in the code area and run it as a JUnit test.

+1
source share

My answer to converting selenium to j unit test is pretty simple.

First of all, you need to configure the code in a promising work environment using the editor, clicking on the toolbar> go to the one shown in> then click on one of these editors> you will see a Java editor or a window editor, etc., this will convert your code . then click go back to the class and select it, right-click and run it as a Java application. You should be able to see your design and source code. more questions

-one
source share

All Articles