簡體   English   中英

從IDE生成的基本Selenium Java無法正常工作

[英]Basic Selenium Java generated from IDE not working

我已經從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>

有時可能是因為使用了舊的硒罐

http://code.google.com/p/selenium/downloads/list下載最新的獨立jar。

嘗試使用新庫,看看是否可行。

在您的testSelenium方法中,它將文本鍵入搜索字段,但從不按Recherche Google搜索按鈕。 該步驟在您的IDE腳本中:

<tr>
 <td>click</td>
 <td>btnG</td>
 <td></td>
</tr>

應該將其提取為如下形式:

driver.findElement(By.id("gbqfba")).click();

您是否出於某種原因從Eclipse中的腳本中刪除了它?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM