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>

Above is how the HTML element is embedded..! The HTML element is a 'New' button with a '+' sign next to it...If, I click on the '+' only, I can get the menu options, which are something like, 'D', 'P', 'T' and 'U'. If I click on the 'New' button ( In the code, it is this part, New , nothing gets displayed as the click action, takes place on the center... Below is the image of the button...

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'

Here is the code, which I have tried...For the last couple of hours,

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

*/

Code is getting executed successfully and test case is pass, but the element which should be seen, when clicked on the '+' button are not seen!

Manikandan
  • 417
  • 4
  • 8
  • 18
  • I'm not sure of what you are trying to do but I suggest you use selenium actions to click on buttons. For information about the Actions class - selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/interactions/Actions.html – Hari Reddy Jun 12 '12 at 10:53
  • @HariReddy, I have added a picture of the button and here is the story..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 are, 'D', 'P', 'T', 'U'...Now, I am aware of Actions, but I will like to see a live code of how it works...I got some errors, when I used a click event within it..and how to give offset value for click event, to the position...If you can provide an example of how you can solve the case using the method, it will be helpful for me.. – Manikandan Jun 12 '12 at 11:27
  • also, notably, finding the element in the page is also pretty difficult...when i run the code, it is telling element not found...whether its because of embedding within em-class, I am not sure...the page has been loaded completely...So, two problems, 1) finding the element 2) Clicking on this element using co-ordinates... – Manikandan Jun 12 '12 at 11:39
  • 1
    Your code is a hot mess, plese, try to paste only the code that matters to the question. You really shouldn't mix Selenium RC and Selenium WebDriver together if you don't have a good reason to do it. Also, in one of the former code versions, I saw [`wait()`](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#wait%28%29) method being used. That does _absolutely_ not what you think it does, it doesn't even belong to Selenium API. Start slow, learn to use Selenium basics on a simple page - HTML only. That's the way you'll get your desired results as soon as possible. – Petr Janeček Jun 12 '12 at 18:55
  • 1
    @Slanec, yes the code was a messy one..! I had to do the work that is assigned...If you have seen my previous posts, I tried a couple of things on Washington Post and a Form submission...However, this is somewhat different, difficult and challenging...Hopefully try to complete this !:) I have modified the code and editing the post ! – Manikandan Jun 12 '12 at 21:37
  • 1
    @Slanec, I am still editing the code...Guess, SeleniumBackedWebDriver might be a good option, in case for some wait methods, where some elements are not properly loaded...When I give Implicit waits for 90 seconds, too, the button click event for 'New' is getting triggered during the page loading itself...thereby making the sub menu item visibility a less chance...but, still the test case is a success...!! tried toggle breakpoint, couldn't figure out how the test case might pass...So, far edited code will be there in the question now.. – Manikandan Jun 12 '12 at 22:00

2 Answers2

14

Use Actions in the following way -

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

Probably by using offset you can exactly make the selenium driver click on the + button you have mentioned.

Hope this helps you.

Hari Reddy
  • 3,808
  • 4
  • 33
  • 42
  • thanks! The element is not been able to be located...with the id...should the class also be included, or something else, if so, can you please put the way the object is located too, in the code? – Manikandan Jun 12 '12 at 12:35
  • 1
    Try using xpath as edited in the code above. If that does not work , let me know ? – Hari Reddy Jun 12 '12 at 12:57
  • the issue is I am using a Linux OS and Eclipse in it and second I am trying with VMWare for Window 7, I am not sure, whether it is related to the environment, that the code is running odd...Sometimes the Login Button is getting clicked, sometimes it is not getting clicked...If and only if the Login button is clicked, I can proceed to the next step...!! I am stuck there... – Manikandan Jun 12 '12 at 13:17
  • 1
    Can you tell me if there are any exceptions that are thrown when you are unable to click the button. This might be very important in solving your problem. – Hari Reddy Jun 12 '12 at 13:38
  • stacktrace attached! And the error happens, after adding the action events! – Manikandan Jun 12 '12 at 18:34
  • 1
    This is the right solution for the question. The exception you are getting is because `waitForCondition()` takes a JavaScript snippet as it's argument, not a piece of Java code. [Try this to resolve it.](https://www.google.cz/search?q=selenium%20wait%20for%20element%20present), or, better yet, with WebDriver, [you can do this](http://seleniumhq.org/docs/04_webdriver_advanced.html#explicit-and-implicit-waits). – Petr Janeček Jun 12 '12 at 19:02
  • @HariReddy,testcase is pass now, but the click event is not getting triggered successfully...It is not displaying any values, upon the mouse click event...I have clicked on the '+' sign...I could see from the firebug element's size...Could it be because of Mouse Listners or something?? – Manikandan Jun 13 '12 at 12:08
  • @Slanec could you also please help with this? – Manikandan Jun 13 '12 at 12:08
  • @Slanec, I have used sahi to finally resolve the problem...Please look into my answer... – Manikandan Jun 18 '12 at 19:03
  • @HariReddy I have used sahi to resolve the problem...That provided the perfect solution...Please, have a look at my answer given...for extjs, it gave me the perfect solution...! – Manikandan Jun 18 '12 at 19:05
  • Pretty obvious but I should add: If your target is on the right-side, Subtract a small amount of pixels from the `ele.Size.Width` and put into the `offsetX` value. – bmkorkut Aug 03 '15 at 14:55
-3

Using the following command _click(_xy(_cell("New"),-5,5)); in sahi solved my problem...!!!

Manikandan
  • 417
  • 4
  • 8
  • 18