3
<table id="ext-comp-1389" class="x-btn x-btn-text-icon " cellspacing="0" style="width: auto;">
<tbody class="x-btn-small x-btn-icon-small-left">
<tr>
<tr>
<td class="x-btn-ml">
<td class="x-btn-mc">
<em class="x-btn-split" unselectable="on">
<button id="ext-gen128" class="x-btn-text create" type="button">New</button>
</em>
</td>
<td class="x-btn-mr">
<i>&nbsp;</i>
</td>
</tr>
<tr>
</tbody>
</table>

上記は、HTML要素の埋め込み方法です。HTML要素は「新規」ボタンで、横に「+」記号が付いています...「+」のみをクリックすると、「D」、「P」などのメニューオプションが表示されます。 '、'T'および'U'。「新規」ボタンをクリックすると(コードでは、この部分、「新規」クリックアクションとして何も表示されず、中央で発生します...以下はボタンの画像です...

中央または任意の場所にある[新規]ボタンをクリックしても、イベントは発生していません。 「+」をクリックすると、「D」、「P」、「T」、「U」として指定したオプションのリストが表示されます。

これが私が試したコードです...ここ数時間、

    package com.hr.testpack;
import static org.junit.Assert.fail;
    import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
    import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
    import com.thoughtworks.selenium.*;

public class Classtest2 {

static Selenium selenium;
static final String HOST = "localhost";
static final int PORT = 4444;
static final String BROWSER = "*firefox";
static final String URL = "http://test.test.com";
private static int buttonwidth=42;//value got from firebug computation tab...
    private static final int Xoffset = (buttonwidth/2)+6;
private static final int Yoffset = 0;
private WebDriver driver;
private String baseUrl;

private StringBuffer verificationErrors = new StringBuffer();
//private StringBuffer verificationErrors = new StringBuffer();

@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://test.test.com/RMprojectProject";
    driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}


@Test

public void testrr1() throws Exception
{


/*  Only Driver...Driver's way to open a URL*/

driver.get(baseUrl + "/");  


/* Webdriver Backed Selenium method- 

//*selenium = new WebDriverBackedSelenium(driver, baseUrl);

//*selenium.open("/");

//selenium.wait(15000); - Never use Wait, when Implicit wait is used


//WebDriver driver = ((WebDriverBackedSelenium) selenium).getWrappedDriver();

//Driver should be instantiated for using Driver's methods for

// SeleniumBackedWebdriver

     // Comments over
     */


driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("test@test.com");

driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("test124");


/* For normal Selenium RC and SeleniumBackedWebDriver method

selenium.type("username", "test");
selenium.type("password","test124");

*/

/* Internal Wait methods, which can be used for normal web
 * 
 * applications. Methods written in the end of the program
 * 
 * waitFor("xpath=.//*[@id='loginButton']/tbody/tr[2]/td[2]'");
 waitSecond(20); */


driver.findElement(By.xpath(".//*[@id='loginButton']/tbody/tr[2]/td[2]")).click();

//selenium.waitForPageToLoad("85000");

//selenium.waitForCondition("selenium.isElementPresent(\"xpath=.//*[@id='ext-gen165']/div/table/thead/tr/td[3]\")", "70000");



WebElement ele = driver.findElement(By.xpath("//*[@id='ext-gen128']"));
Actions build = new Actions(driver);

build.moveToElement(ele, Xoffset, Yoffset).click().build().perform();




    //  selenium.waitForCondition("selenium.isElementPresent(\"xpath=//*[@id='ext-gen246']\")", "20000");

driver.findElement(By.xpath("//*[@id='ext-gen245']")).click();

  }



@After

public void tearDown() throws Exception{

    selenium.stop();
    //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;
    }
}

/* The Below methods can be used, for page loading/waits in 
 * 
 * normal web applications...waitFor(seconds),when called in
 * 
 * the application, the wait happens...useful in occasions

public void waitFor(String locator) throws Exception {
    for (int second = 0;; second++) {
        if (second >= 60)
            fail("timeout");
        try {
            if (selenium.isVisible(locator))
                break;
        } catch (Exception e) {
        }
        Thread.sleep(1000);
    }
}

public void waitSecond(int n) throws Exception {
    for (int second = 0;; second++) {
        if (second >= 60)
            fail("timeout");
        try {
            if (second > n - 1)
                break;
        } catch (Exception e) {
        }
        Thread.sleep(1000);
    }
}

*/

コードは正常に実行され、テストケースは合格ですが、「+」ボタンをクリックしたときに表示されるはずの要素は表示されません。

4

2 に答える 2

14

次のようにアクションを使用します-

    WebElement ele = driver.findElement(By.xpath("//*[@id='ext-gen128']");
Actions build = new Actions(driver);
build.moveToElement(ele, Xoffset, Yoffset).click().build().perform();

おそらくオフセットを使用することで、セレンドライバーにあなたが言及した+ボタンをクリックさせることができます。

これがお役に立てば幸いです。

于 2012-06-12T11:44:16.670 に答える
-3

次のコマンドを使用する_click(_xy(_cell( "New")、-5,5)); サヒで私の問題を解決しました...!!!

于 2012-06-18T19:03:02.557 に答える