如何使用Selenium单击Button的特定部分,以显示要显示的选项列表?

时间:2012-06-12 09:57:28

标签: selenium webdriver selenium-rc junit4 sahi

<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'。如果我点击“新建”按钮(在代码中,就是这部分,,点击操作不会显示在中心...下面是按钮的图像......

When I click on the New button in the middle or any place, no events are happening. When I click on the '+', the list of options are displayed, which I had given as, '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);
    }
}

*/

代码正在成功执行并且测试用例已通过,但是在单击“+”按钮时应该看到的元素不会被看到!

2 个答案:

答案 0 :(得分:12)

以下列方式使用操作 -

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

可能通过使用偏移量,您可以完全使硒驱动器单击您提到的+按钮。

希望这会对你有所帮助。

答案 1 :(得分:-2)

使用以下命令_click(_xy(_cell(“New”), - 5,5));在sahi解决了我的问题... !!!