如何在Java中执行Selenium测试

8
我用Selenium IDE创建了一个自动化测试案例。我想为该案例创建一些循环/控制流程,因此我决定将其导出到类似于Java的东西中(我最熟悉Java)。我导出到了Java/JUnit4/Web Driver。我认为通过Eclipse尝试执行java文件是最好的方法,但如果有更简单的方法,请告诉我。无论如何,我找不到关于如何通过Eclipse执行此Java的好的解释。
大多数我读到的东西都告诉我要确保我的Build Path库包括Selenium Standalone Server。几乎所有我读到的东西都告诉我要使用Selenium Remote Control。然而,我认为RC已经过时了,我想知道是否有任何方法使其与我从Selenium下载的更近期的Web Driver stuff一起工作。此外,大多数我读到的东西都告诉我需要使用public static void main(),这有点棘手,因为我不知道如何修改导出的selenium给我的代码(显然我不能只将它全部粘贴到主方法中)。
如果有人能够从Selenium导出到Java再执行代码,我将永远感激不尽。
Selenium给我的代码: 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;
      }
   }
}

这让我产生了4个错误(其中3个是注释的错误,我可以删除它们,另一个是tearDown()方法中的fail。我关心的不是这些错误,而是如何使这段代码真正执行?
谢谢!

正在为您准备答案--您是否可以展示从Selenium IDE导出的Java代码?这并非完全必要,但它可能有助于我更清晰地回答您的问题。 - James Dunn
1
这是一个完全工作的Java代码文件。我不太确定它有什么复杂之处?下载并安装JUnit到您的项目中,它会照顾好一切...像这样的文档:https://code.google.com/p/selenium/wiki/GettingStarted...只需使用`main`方法即可避免添加额外的测试框架。就这些。 - Arran
我昨天刚刚发布了一个很好的入门项目,适合像你这样的人了解事物的工作原理。- https://github.com/ddavison/selenium-framework 我使用jUnit来执行测试。也许这会让你对它有更深入的了解。 - ddavison
@Arran,Before、After和Test注释的错误都只是说“...无法解析为类型”。对于fail方法更令人担忧的错误是“The method fail(String) is undefined for the type RackspaceContactAutomation”。 - Man Friday
1
说实话,这些错误给了你一个提示。提示你缺少这个难题的一个重要部分。你将测试导出为 JUnit 测试,但实际上没有导入 JUnit。下面的答案展示了如何做到这一点。所以不要忽略那些错误。它们告诉你哪里出了问题。 - Arran
显示剩余2条评论
3个回答

14
一个在Eclipse中运行Selenium Java代码的好方法是将其作为JUnit测试运行。

1. 在Eclipse中创建一个Maven项目。如果您以前没有这样做过,请参见:

2. 将以下依赖项添加到您的pom.xml文件中:

<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. 将导出的Java文件复制到Maven项目中。

4. 在文件中添加以下导入内容:

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

5. 将Java文件作为JUnit测试运行,如下所示:

我的Eclipse示例(版本为Kepler)


如果我的回答中有任何不清楚的地方,请告诉我,我很乐意添加解释并进行更新。 - James Dunn
已经注意并更改。谢谢! - James Dunn
@user2719370,我在答案中放了两个链接,一个是安装Eclipse中的Maven,另一个是设置项目。 (第一个链接是我刚刚回答另一个问题时做的,所以我有好的链接可以使用。如果你觉得它有帮助,请点赞!)另外,如果有其他需要帮忙的地方,请告诉我,我很乐意帮助。 - James Dunn
@user2719370 这很奇怪。我会看看能找到什么。你使用的Eclipse版本是什么? - James Dunn
@ManFriday 尽管这并不能解释奇怪的错误,但如果您正在使用 Kepler,则 Maven 应该已经与 Eclipse 集成。您是否尝试过先制作一个 Maven 项目而不是首先安装 Maven? - James Dunn
显示剩余5条评论

1
之前的答案都是正确的。但是要直接从Eclipse中运行,您需要对代码进行一些修改。您不需要使用public void main来运行junit代码。因此,以下是使您能够将代码复制并粘贴到Eclipse中并作为JUnit测试运行的步骤:
  1. 在Eclipse中安装JUnit ->帮助 -> Eclipse市场 ->搜索JUnit并安装它,然后重新启动Eclipse。

  2. 在Eclipse中创建一个项目和一个新包,然后创建一个与您的Selenium IDE导出代码相同的名称的新类,删除除包行之外的所有内容。

  3. 将代码从Selenium IDE复制并粘贴到该类中,删除包行。

  4. 右键单击您的代码区域并作为JUnit测试运行。


-1

将selenium转换为j unit测试的答案非常简单。

首先,您需要在透视工作台中设置代码,然后通过单击工具栏中的“显示”>“然后按一个这些编辑器”来使用编辑器>您会看到Java编辑器或窗口编辑器等,这将转换您的代码。然后返回类并将其突出显示,右键单击鼠标并将其运行为Java应用程序。您应该能够看到您的设计/源代码。还有其他问题吗?


请考虑将你的答案格式化为更易读的格式。 - Benjamin Gruenbaum

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接