Decision making with Selenium

Submitted by aaron on Mon, 04/13/2009 - 21:52

 I recently needed to create a test case for selenium that required a user to have just logged in to a Drupal website.  Often when running selenium tests, the browser may already have an authenticated session, so the test will need to detect this and then logout.  Of course with Drupal you can always just go to the url ?q=logout to make sure you are logged out, but this way is more fun and makes a good example for using logic in Selenium tests!

 

EnsureLoggedOut
open ../index.php?q=user/login  
storeElementPresent //*[@id="edit-submit"] loginButtonPresent
echo javascript{storedVars['loginButtonPresent']}  
storeEval var destinationURL = null; if(storedVars['loginButtonPresent']==true){   destinationURL = "../index.php?q=user/login"; } else{   destinationURL = "../index.php?q=logout"; } destinationURL
echo javascript{storedVars['destinationURL']}  
open ${destinationURL}  

 

The first row, goes to the login URL.  If the user is not logged in, they will see the login form which includes the submit button.  So, the second row looks for that submit button.  The third row displays the result.  In the fourth row I use the storeEval action with some JavaScript logic to tell the script to either go to ?=logout, or to stay on the ?q=user/login page.  It's pretty simple once you piece together how to get storeEval to work.

Copyright © 2011, Aaron Blondeau

Drupal theme by Kiwi Themes.