Man*_*day 8 java eclipse selenium
所以我使用Selenium IDE为我想要的一些自动化创建一个测试用例.我希望能够创造这种情况下,一些循环/流量控制,所以我想我会需要将其导出了硒IDE的东西像Java(我最熟悉Java).我导出到Java/JUnit4/Web Driver.我认为尝试通过Eclipse执行java文件效果最好,但如果有人知道更容易的事情,请告诉我.无论如何,我发现如何通过Eclipse执行这个Java没有好的解释.
我读过的大多数内容都告诉我要确保我的Build Path库包含Selenium Standalone Server.事实上,我读过的所有内容都告诉我使用Selenium遥控器.但是,我认为RC是折旧的,我想知道是否有任何方法可以使它与我从Selenium下载的最近的Web Driver一起工作.另外,我读过的大多数东西告诉我我需要使用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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我4个错误(3注释,这我可以删除,一个是fail在tearDown()方法,这不是我关心的错误,那么多我怎么让这段代码实际执行?
谢谢!
Jam*_*unn 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>
Run Code Online (Sandbox Code Playgroud)
3.将导出的Java文件复制到Maven项目中.
4.将以下导入添加到文件中:
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Run Code Online (Sandbox Code Playgroud)
5.将Java文件作为JUnit测试运行,如下所示:

| 归档时间: |
|
| 查看次数: |
52337 次 |
| 最近记录: |