gpt4 book ai didi

java - 从 IDE 生成的基本 Selenium Java 不工作

转载 作者:行者123 更新时间:2023-11-30 04:23:41 25 4
gpt4 key购买 nike

我从 Selenium IDE 生成了这个脚本。它在 IDE 中运行,而不是在 Eclipse 中运行:它仅运行 Firefox 20 并访问 Google。但它不会搜索任何内容。

更新:我用 Thread.Sleep(3000L) 替换了 setSpeed但我不知道如何处理错误注释“定位器策略”

package Selenium;

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 Selenium {
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 = "http://www.google.fr/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void testSelenium() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("selenium");
// ERROR: Caught exception [ERROR: Unsupported command [setSpeed | 3000 | ]]
// ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]
driver.findElement(By.linkText("Selenium - Web Browser Automation")).click();
// Warning: assertTextPresent may require manual changes
assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*What is Selenium[\\s\\S][\\s\\S]*$"));
}

@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;
}
}
}

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="http://www.google.fr/" />
<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>id=gbqfq</td>
<td>selenium</td>
</tr>
<tr>
<td>setSpeed</td>
<td>3000</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>btnG</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>link=Selenium - Web Browser Automation</td>
<td></td>
</tr>
<tr>
<td>assertTextPresent</td>
<td>What is Selenium?</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>

最佳答案

这有时可能是因为使用旧的 Selenium jar

http://code.google.com/p/selenium/downloads/list 下载最新的独立 jar

尝试使用新库,看看是否有效。

关于java - 从 IDE 生成的基本 Selenium Java 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16401732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com