MorkPork
MorkPork

Reputation: 884

Selenium Webdriver click() not doing anything

Should be simple enough, I have the following code: -

DesiredCapabilities capabilities = new DesiredCapabilities();
DesiredCapabilities.internetExplorer();
capabilities.setCapability("nativeEvents",false);

WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capabilities);

// And now use it
driver.get("dvfow01.mySite.com");
Thread.sleep(5000);

driver.findElement(By.linkText("Create")).click();
System.out.println("test");
driver.findElement(By.id("CreateDeal")).click();

It clicks the first item (linkText of "Create") but never gets to the next line in the sequence. The moment I close the browser it hits the next line, printing out the text "test" and then obviously immediately crashes due to no element on closed window.

Any suggestions? I'm running all the latest versions...

The clicking of this link expands a hidden section via tagit.js so it looks like a drop down but isn't... the html of the page looks like so...

  <li class="dropdown"><a href='#' class='collermenu dropdown-toggle' data-toggle='dropdown'>Create <b class='caret'></b></a>
        <ul class="dropdown-menu">
            <li><a id="createDeal">Deal</a></li>
            <li><a id="second">someting</a></li>
            <li><a id="third">something else</a></li>
            <li><a id="fourth">a fourth</a></li>
            <li><a id="fifth">the last</a></li>
        </ul>
    </li>

I've spoken with my devs and they tell me that they are running something called signalr with foreverframe to continually comminucate back to the server for updates/popups etc?

Anyone heard of this or used it in conjunction with selenium?

Upvotes: 1

Views: 4257

Answers (3)

testing
testing

Reputation: 1796

Try this sample code:

JavascriptExecutor executor= (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('ID').style.display='block';");
Select select = new Select(driver.findElement(By.id("ID")));
select.selectByVisibleText("value");
Thread.sleep(6000);

By using java script executor and make the element visible then click on the element through ID. Hope it hepls..

Upvotes: 0

MorkPork
MorkPork

Reputation: 884

The only solution I could come up with for this was to change the method by which the devs utilised signalR in our dev environment. They were using foreverframe but this just doesn't play ball with selenium, so instead I had to get them to change back to long polling. Not ideal to have different configurations in dev vs live but it gets round the issue...

Upvotes: 1

ddavison
ddavison

Reputation: 29092

It clicks the first item (linkText of "Create") but never gets to the next line in the sequence.

That usually stems from an application issue. WebDriver will wait until the page is fully loaded, and my guess is that it never does fully load. there is an app issue, which would explain why after you close it, it echo's that.

I'm willing to bet too, that if you waited long enough, Selenium would spit an exception.

A suggestion i'd make, is to clean up your code first off..

Don't use a Hub unless you expect it to be a dedicated server. If you are running tests on your local machine, just use a local webdriver instance.

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability("nativeEvents",false);

WebDriver driver = new IEDriver(capabilities);

driver.get("http://myurl.com");

Second, take out, and LEAVE out all explicit waits like Thread.sleep(). Selenium has what's called Implicit waits. If these aren't enough, write your own method like waitForElement(By) which will wait for something to appear.

Also, it might help to explain what happens after clicking that <a href="someurl">Create</a>

Edit

After your edits, i can give you some more pointers. If your scenario is..

  1. Click the link.
  2. Click an item in the dropdown.

Then you can do something like -

driver.findElement(By.cssSelector("li.dropdown > a[data-toggle='dropdown']")).click();
driver.findElement(By.cssSelector("li.dropdown li > a#createDeal")).click();

As I am not fond of doing these manual findElements, i'd like to share with you a great project to help you get started with Selenium WebDriver Java. Getting started with selenium (java) on GitHub

It is a pretty functional project that includes a fluent style interface, and abstracts you away from all these driver.findElement()'s. Your test would look something like,

@Config(url="http://mysite.com", browser=Browsers.IE)
public class MyTest extends AutomationTest {
    @Test
    public void test() {
        // we will be at mysite.com right now.
        click(By.cssSelector("li.dropdown > a[data-toggle='dropdown']")
        .click(By.cssSelector("li.dropdown li > a#createDeal"))
        .validatePresent(By.id("someIdThatAppearsAFterClicking"));
    }
}

Upvotes: 3

Related Questions