1

I'm in the SeleniumIDE , but calling out to javascript.

Seems like this would be a fairly common scenario for others too.

I have a good test suite but the first thing it does is login.

I would like the suite to start off be making sure I am logged out and if not, logging me out. I can tell if I am logged in by the presence of a 'Logout' hyperlink

But I only want to click on logout IF I am currently logged in, otherwise I want to do nothing, as trying to click on a non-existent element would raise an error if I am not already logged in)

So logically this is:

if ui element(logout link in my case) exists
  click on logout link
else
  do nothing
end

I am using the Selenium IDE and calling javascript - Given that I can't do if then in the basic seleniumIDE I was hoping I could do this in javascript itself.

something like:

store    javascript{if ([a with text 'Logout' exists]) then click on it end;}  id1

although instead of click on it [this], it would also be ok (though more brittle) if I just visited the url which is

http://my-apps-domain/users/sign_out

but I'm not sure of the exact syntax.

The relevant HTML is:

<li><a href="/users/sign_out">Logout</a></li>

If it exists I would like to click on the a (or visit the url directly), otherwise nothing.

I would like to find a non-jquery solution if possible.

Update: I have found that even javascript{window.location.replace('http://google.com') } closes my seleniumIDE window and replaces it with google but doesn't affect the actual window where the tests themselves were running.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497

2 Answers2

3

Triggering a click event in raw JavaScript can be tricky (check out this answer: https://stackoverflow.com/a/10339248/2386700)

However, if you can also use jQuery, that would simplify things. For example, if the logout button has an id like "logout" then you could do something like this:

var logoutButton = $('#logout');

if (logoutButton != null) {
    logoutButton.click();
}
Community
  • 1
  • 1
Dingredient
  • 2,191
  • 22
  • 47
0

Since you don't have control over the HTML, I suggest referencing the link in another manner. The URL seems very reliable for that purpose:

var logoutLink = document.querySelector('a[href="/users/sign_out"]');
if(logoutLink != null) {
    window.location.href = logoutLink.href;
}

You don't need to fire any kind of click event, because page navigation can easily be done with window.location.

UPDATE:

Another idea is to assign your button an id, then click it with selenium:

var logoutLink = document.querySelector('a[href="/users/sign_out"]');
if(logoutLink != null) {
    logoutLink.setAttribute("id", "logoutLink");
}
MCL
  • 3,985
  • 3
  • 27
  • 39
  • Unfortunately this seems to a) Close the firefox browser seleniumIDE window being used by the test suite. b) Ignore the actual browser window and c) Open a new browser instance with the logout page. The one thing it does NOT do is a logout on the firefox window instance that the selenium driver is using! – Michael Durrant Jun 12 '13 at 19:11
  • @MichaelDurrant does the update work for you? I don't know much about selenium, but I guess you do. – MCL Jun 12 '13 at 20:14