51Testing软件测试论坛
标题:
IDE录制转Junit4 webdriver的java代码只能打开Firefox求原因
[打印本页]
作者:
windanchaos
时间:
2015-9-24 14:15
标题:
IDE录制转Junit4 webdriver的java代码只能打开Firefox求原因
本帖最后由 windanchaos 于 2015-9-24 14:18 编辑
使用selenium IDE录制打开百度网页,输入关键词并查找,然后打开搜索结果的网页。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="https://www.baidu.com/" />
<title>baidu</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">baidu</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>id=kw</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>id=kw</td>
<td>周杰伦</td>
</tr>
<tr>
<td>click</td>
<td>id=su</td>
<td></td>
</tr>
<tr>
<td>open</td>
<td>/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=%E5%91%A8%E6%9D%B0%E4%BC%A6&rsv_pq=8a16b64b00009083&rsv_t=9a0e0vTsQQyLB4FqW58OWK6O6pv7OKJ42gHcR7MtcYs7VgkirsM8iHDIlA4&rsv_enter=1&rsv_sug3=1</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>link=周杰伦_百度百科</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>
复制代码
接着另存为java JUnit4 webdriver 代码,并修改其中的乱码。
package com.selenium.Glen;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class baidu {
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://www.baidu.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testBaidu() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("kw")).click();
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("周杰伦");
driver.findElement(By.id("su")).click();
driver.get(baseUrl + "/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=%E5%91%A8%E6%9D%B0%E4%BC%A6&rsv_pq=8a16b64b00009083&rsv_t=9a0e0vTsQQyLB4FqW58OWK6O6pv7OKJ42gHcR7MtcYs7VgkirsM8iHDIlA4&rsv_enter=1&rsv_sug3=1");
driver.findElement(By.linkText("周杰伦_百度百科")).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;
}
}
}
复制代码
然后在Eclipse当中运行,只能把firefox打开,之后的步骤都不能跑。
不知道是啥原因。
我使用的selenium版本是selenium-java-2.44.0.jar
请有经验的同学指点一二啊,瓶颈了,多谢多谢
作者:
windanchaos
时间:
2015-9-24 17:27
问题解决,是Firefox版本和selenuim的兼容问题,导致的,将版本改为最新的即解决问题。
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2