java - 如何在 Java 中执行 Selenium 测试

标签 java eclipse selenium

所以我使用 Selenium IDE 为我想要完成的一些自动化创建了一个测试用例。我希望能够为这种情况创建一些循环/流控制,所以我想我需要将它从 Selenium IDE 导出到类似 Java 的东西(我最熟悉 Java)。我导出到 Java/JUnit4/Web Driver。我认为尝试通过 Eclipse 执行 java 文件效果最好,但如果有人知道更简单的方法,请告诉我。不管怎样,我没有找到关于如何通过 Eclipse 执行这个 Java 的很好的解释。

我读到的大部分内容都告诉我要确保我的构建路径库包含 Selenium 独立服务器。实际上,我阅读的所有内容都告诉我使用 Selenium Remote Control。但是,我认为 RC 已经贬值了,我想知道是否有办法让它与我从 Selenium 下载的更新的 Web 驱动程序一起工作。此外,我读到的大部分内容都告诉我我需要使用 public static void main(),这有点尴尬,因为我不知道如何更改导出的 selenium 给我的代码(显然我不能将其全部粘贴到 main 方法中)。

如果有人能指导我从将 Selenium 导出到 Java 到执行代码,我将永远感激您。

Selenium 给我的代码: 包 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。这不是我的错误我非常关心如何让这段代码真正执行?

谢谢!

最佳答案

在 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 测试运行,如下所示:

Example from my Eclipse (Version is Kepler)

关于java - 如何在 Java 中执行 Selenium 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18492668/

相关文章:

java - 如何使用java获取连接到LAN的所有系统名称?

java - Scala SBT 失败 - 加载 CharSequence 时出错(Scala 2.12.1,SBT 0.13.13)

java - Android-Java 列表到字符串,反之亦然

eclipse - 从 diskfileupload 上传文件时出错

java - 将动态 Web 项目导入 Eclipse

java - 使用现有 Java 文件在 Eclipse 上创建新项目

Python 2.7 Selenium 网站上没有这样的元素

java - java中自动转换是如何工作的?

java - 在 debian 8 上使用 chromedriver 启动 Selenium 后出错

python-3.x - 如何让 Selenium 记住登录/ session