简体   繁体   English

如何在Java中执行Selenium测试

[英]How to execute a Selenium test in Java

So I used Selenium IDE to create a test case for some automation I want done. 所以我使用Selenium IDE为我想要的一些自动化创建一个测试用例。 I want to be able to create some looping/flow control for this case so I figured I would need to export it out of Selenium IDE to something like Java (I'm most familiar with Java). 我希望能够为这种情况创建一些循环/流量控制,所以我想我需要将它从Selenium IDE导出到像Java这样的东西(我最熟悉Java)。 I exported to Java/JUnit4/Web Driver . 我导出到Java / JUnit4 / Web Driver I think trying to execute the java file through Eclipse would work best, although if someone knows something easier, let me know. 我认为尝试通过Eclipse执行java文件效果最好,但如果有人知道更容易的事情,请告诉我。 Anyway, I have found NO GOOD EXPLANATION on how to execute this Java through Eclipse. 无论如何,我发现如何通过Eclipse执行这个Java没有好的解释。

Most things I read tell me to make sure my Build Path libraries includes the Selenium Standalone Server . 我读过的大多数内容都告诉我要确保我的Build Path库包含Selenium Standalone Server Virtually all things I read tell me to use the Selenium Remote Control. 事实上,我读过的所有内容都告诉我使用Selenium遥控器。 However, I thought the RC was depreciated, and I am wondering if there is anyway to make it work with the more recent Web Driver stuff I downloaded from Selenium. 但是,我认为RC是折旧的,我想知道是否有任何方法可以使它与我从Selenium下载的最近的Web Driver一起工作。 Also, most things I read tell me I need to use public static void main() , which is a little awkward because I don't know how to alter the code the exported selenium gives me (obviously I can't just paste it all in the main method). 另外,我读过的大多数东西告诉我我需要使用public static void main() ,这有点尴尬,因为我不知道如何改变导出的selenium给我的代码(显然我不能只是粘贴它在主要方法)。

If anyone could walk me through from exportation of Selenium to Java to executing the code, I will be forever in your debt. 如果有人可以指导我从Selenium出口到Java再到执行代码,我将永远负债累累。

The code Selenium gives me: package com.example.tests; 代码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;
      }
   }
}

This gives me 4 errors (3 for the annotations, which I could just delete, and one for fail in the tearDown() method. It's not the errors I'm concerned about so much the how do I make this code actually execute? 这给了我4个错误(3个用于注释,我可以删除,1个用于tearDown()方法中的fail 。这不是我非常关心的错误,我如何使这个代码实际执行?

Thanks! 谢谢!

A good way to run Selenium Java code in Eclipse is to run them as JUnit tests . 在Eclipse中运行Selenium Java代码的一种好方法是将它们作为JUnit测试运行。

1. Create a Maven Project in your Eclipse. 1.在Eclipse中创建Maven项目。
If you haven't done this before, see: 如果您之前没有这样做,请参阅:

2. Add the following dependencies to your pom.xml file: 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. Copy your exported Java file into the Maven Project. 3.将导出的Java文件复制到Maven项目中。

4. Add the following imports to the file: 4.将以下导入添加到文件中:

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, like so: 5.将Java文件作为JUnit测试运行,如下所示:

我的Eclipse示例(版本是开普勒)

The previous answer are all legitimate. 以前的答案都是合法的。 But to run directly from your eclipse you need do some modifications to your code. 但是要直接从你的eclipse运行,你需要对你的代码进行一些修改。 You dont need public void main to run a junit code. 你不需要public void main来运行junit代码。 So Here are the steps to enable you to just copy over the code and paste it in eclipse and run as JUnit test: 以下是使您能够复制代码并将其粘贴到eclipse中并以JUnit测试运行的步骤:

  1. Install JUnit in eclipse->Help->eclipse market place-> search for JUnit and install it, restart eclipse. 在eclipse-> Help-> eclipse market place中安装JUnit->搜索JUnit并安装它,重启eclipse。

  2. Create a project in eclipse and a new package, then create a new class with the same name as your selenium IDE exported code, delete everything except the package line. 在eclipse中创建一个项目和一个新包,然后创建一个与selenium IDE导出代码同名的新类,删除除包行以外的所有内容。

  3. copy and paste the code from selenium IDE to that class, remove the package line. 将selenium IDE中的代码复制并粘贴到该类,删除包行。

  4. Right click in your code area and run as JUnit test. 右键单击代码区域并以JUnit测试运行。

My answer to converting selenium in to aj unit test, is quite simple. 我将硒转化为aj单元测试的答案非常简单。

1st you need to set up the code in the perspective workbench the use the editor by clicking on in the tool bar >go to shown in>then press on one of these editor > you see Java editor or window editor and so on, this will convert your code. 首先你需要在透视工作台中设置代码,在工具栏中单击使用编辑器>转到>>然后按下其中一个编辑器>你看到Java编辑器或窗口编辑器等等,这将是转换你的代码。 then press go back to the class and highlight it, right click on the mouse and run it as Java application. 然后按回到类并突出显示它,右键单击鼠标并将其作为Java应用程序运行。 you should be able to see your design/and source code. 你应该能够看到你的设计/和源代码。 anymore questions 再问题了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM