1

I am trying to develop a test for a website. Normally, it would go Main page, click search button to results page, Click first link to product page.

However, sometimes when loading the first page, you get a dialog popup that asks the user their location to display better content.

I'm relatively new to Selenium, but I've done a decent amount of research on this and can't seem to find the right answer. Maybe I just don't know exactly what to search for.

How can I do a conditional that says If the box pops up, click the first option, and then EITHER way, check content of the page that would normally come up?

Note that I DO know how to test for elements etc, I just don't understand how to do the conditional bit. Also, I export this to Perl after, if that makes any difference.

Thanks in advance!

EDIT: Here's the script as-is.

use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;
use TimePerf2;

my $time= TimePerf2->new(60,90,120);


my $sel = Test::WWW::Selenium->new( host => "my_lan_ip",
                                    port => 4444,
                                    browser => "*firefox",
                                    browser_url => "https://www.website.com/" );

$time->startTime("ALL");
$sel->set_timeout_ok("120000");
if ( !  $sel->open_ok("https://www.website.com/") ) {print "Location: " . $sel->get_location() . "\n";}
elsif ( ! $sel->set_speed("1500") ) { }

elsif ( !  $sel->click_ok("id=btnSearch") ) {print "Location: " . $sel->get_location() . "\n";}

elsif ( ! $sel->wait_for_page_to_load_ok("120000") ) {}
elsif ( !  $sel->is_element_present_ok("css=div.azi_result") ) {print "Location: " . $sel->get_location() . "\n";}

elsif ( !  $sel->click_ok("css=p.description a") ) {print "Location: " . $sel->get_location() . "\n";}

elsif ( ! $sel->wait_for_page_to_load_ok("120000") ) {}

elsif ( !$time->endTime("ALL") ) { }

else { $time->getTimes(); }
$sel->stop();
Bruce
  • 81
  • 9

1 Answers1

0

You can write it this way:

List<WebElement> popUps = driver.findElements(By.yourLocator);
if (popUps.size() > 0) {
    skipPopup(popUps.get(0));
}
...normal flow...

In this fragment we create a list of popups identified by some common locator, and then check if this list is empty. If it is, then no popups were found, and we may proceed with normal flow. If the list is not empty, then we have a popup, which we then skip. The logic of skipping (usually clicking Cancel or the like) is described in skipPopup() method which is fed with the first element of the list. This is a simple variant, since we presume there can be either one popup or zero. If there can be several popups at once, we'll need to go through the list and skip every popup we located. If popups appear one by one, after the previous is closed, we'll need calling findElements inside the loop. It's also useful to change duration of web driver's implicit wait period which is used in findElements method and can affect performance of the test drastically (see explanations and examples here: Selenium WebDriver - Test if element is present).

Community
  • 1
  • 1
Gyrotank
  • 153
  • 1
  • 2
  • 9
  • Hi Gyrotank, thanks for answering. I apologize for being a noob, could you explain what that is doing? Also, what info of my own, besides the locator I'd need to feed in there? – Bruce Apr 28 '17 at 14:49
  • Hi! Added comments to the answer since they turned out too long to fit inside a comment. – Gyrotank Apr 28 '17 at 22:12
  • It should, although it's better to examine the IDE first. – Gyrotank May 02 '17 at 16:00