Roh*_*ish 6 java selenium htmlunit
我使用SeleniumHQ记录我的动作,然后将它们导出到Java Unity WebDrive.然后我编辑了导出的代码并添加了许多额外的东西,比如循环数组,时间戳等.
我的代码如下:
我尝试过使用FirefoxDriver和HtmlUnitDriver,但是每一个都给了我这个奇怪的问题.我的代码开始做它的工作并随机停留在随机点并永远挂在那里.
例如,它可以登录 - >转到配置文件 - >删除先前然后停止,或者它可以挂在登录中.我一遍又一遍地循环这些步骤,更多的循环更可能是卡住了.
第一循环成功率是90%,第二循环是40%左右等.Driver我使用的也影响了这一点.它最有可能挂起,HtmlUnitDriver我真的想要使用,HtmlUnitDrive因为我想在Ubuntu Server上运行我的代码无头.
还有其他人有类似的问题吗?
编辑:经过几个小时的测试,我注意到它只有HtmlUnitDriver挂起而不是Firefox.使用Firefox时,我可以看到它正在做什么,它正在做所有应有的事情.出现问题HtmlUnitDriver.
这是代码本身:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class WebUpdater {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new HtmlUnitDriver(true); // JavaScript enabled.
baseUrl = "http://exampleurl.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testUnity() throws Exception {
openAndLogin();
gotoProfile();
deletePreviousPost();
uploadPost();
logOut();
System.out.println("Done!");
}
private void openAndLogin() {
driver.get(baseUrl);
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("jsid-login-id")).clear();
driver.findElement(By.id("jsid-login-id")).sendKeys("bilgeis.babayan@gmail.com");
driver.findElement(By.id("jsid-login-password")).clear();
driver.findElement(By.id("jsid-login-password")).sendKeys("volume1991");
driver.findElement(By.cssSelector("input.right")).click();
}
private void gotoProfile() {
driver.findElement(By.cssSelector("img[alt=\"Profile\"]")).click();
}
private void deletePreviousPost() {
try {
driver.findElement(By.cssSelector("img[alt=\"ExampleName\"]")).click();
driver.findElement(By.linkText("Delete")).click();
assertTrue(closeAlertAndGetItsText().matches("^Confirm to delete this post[\\s\\S]$"));
} catch (Exception e) {
System.out.println(e);
}
}
private void uploadPost() {
driver.findElement(By.linkText("ExampleAction")).click();
driver.findElement(By.id("example_url")).clear();
driver.findElement(By.id("example_url")).sendKeys("Example text that gets typed in textfield.");
driver.findElement(By.cssSelector("input[name=\"example\"]")).clear();
driver.findElement(By.cssSelector("input[name=\"example\"]")).sendKeys("ExampleName");
driver.findElement(By.linkText("ExampleAction2")).click();
System.out.println("Done");
}
private void logOut() {
driver.get("http://exampleurl.com/logout");
System.out.println("Logged out.");
}
@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 String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alert.getText();
} finally {
acceptNextAlert = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的主类中,我调用WebUpdater类,如下所示:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
Logger logger = Logger.getLogger("");
logger.setLevel(Level.OFF);
scan();
}
private static void scan() {
while (true) {
try {
// Test if connection is available and target url is up.
URL url = new URL("http://exampleurl.com");
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.connect();
// Start tests.
WebUpdater updater = new WebUpdater();
updater.setUp();
updater.testUnity();
updater.tearDown();
} catch (Exception ex) {
System.out.println(ex);
}
try {
Thread.sleep(12000);
} catch (InterruptedException e) {
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我对 HtmlUnitDriver 的体验很糟糕。前段时间我写了一个测试框架,本来应该在 Hudson 上启动的,最后我决定使用 Firefox 驱动程序,它更可预测,更容易调试。关键是,就我而言,这是一个充满 javascript 的页面 - 动态加载的字段等,并且使用 HtmlUnitDriver 确实是一个蠕虫。
如果您确实需要使用 HtmlUnitDriver,请尝试调试“pagesource”,Selenium 在“当前”(挂起)时刻可以访问该页面源。
| 归档时间: |
|
| 查看次数: |
2117 次 |
| 最近记录: |