2

Ok -- The issue seems to be resolved -- see below for a working solution.

THE PROBLEM => Careful with Selenium's Explicit timeout vs Implicit timeout. I'm using Browserstack to test IE remotely, via Selenium's Ruby Bindings, running tests via RSpec.

The elements I'm querying are google ad divs, I want to be sure we're serving ads across all our platforms. Loading is asynchronous, so I'm ensuring I wait for page load.

When I manually drive selenium from the ruby shell (irb), I am always able to find the elements in IE, versions 9, 10, 11. Same when I manually navigate to the site in Browserstack's 'Live' in-browser IE emulation.

BUT -- when I run the same commands as I do in the shell programmatically, via RSpec -- my tests fail about 50% of the time. IE 11 is the hardest hit, failing most tests. IE 10 tends to fare a little better.

Same tests for Firefox + Chrome are passing.

Dependencies:

ruby '2.3.0'
gem 'selenium-webdriver'
gem 'rspec'
gem 'curb', '~> 0.9.1'

Any ideas?

describe "Testing example.com in production with ie." do

    it "loads ads on home page" do

        caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer
        caps["browser_version"] = "11.0"
        caps["os"] = "WINDOWS"
        caps["os_version"] = "8"
        caps["resolution"] = "1024x768"
        caps["browserstack.debug"] = "true"


        driver = Selenium::WebDriver.for(:remote,
          :url => "http://<my_username>:<my_pw>@hub.browserstack.com/wd/hub",
          :desired_capabilities => caps)

        driver.navigate.to "http://www.example.com"
        wait = Selenium::WebDriver::Wait.new(:timeout => 5) # seconds
        verify = []
        begin  #
            wait.until do 
                verify = driver.find_elements(:css => "div[id^="google_ads"]")
            end
        ensure
            driver.quit
        end
        expected_ad_count = 5
        expect( verify.count ).to eq expected_ad_count
    end
end

THE SOLUTION => The explicit wait block was failing for IE. The solution is, after initializing the driver, to configure implicit wait, as follows:

driver = Selenium::WebDriver.for(:remote,
      :url => "http://<my_username>:<my_pw>@hub.browserstack.com/wd/hub",
      :desired_capabilities => caps)

driver.manage.timeouts.implicit_wait = 5
driver.navigate.to "http://www.example.com"
begin  #
   verify = driver.find_elements(:css => "div[id^="google_ads"]")

ensure
    driver.quit
end
expected_ad_count = 5
expect( verify.count ).to eq expected_ad_count

And -- it works!

Avi Fox-Rosen
  • 55
  • 1
  • 6

1 Answers1

1

As per your test, I believe your script will continue execution even if there are no elements located using the find_elements(:css => "div[id^="google_ads"]") command. This is because explicit wait only waits for a successful execution of the command and a null response for the find elements command is considered successful.

I would suggest you replace explicit wait with implicit wait in your test. This will help you wait until all the elements are loaded in the DOM and then continue with test execution. You can use the following command in your test to add Implicit wait:

driver.manage.timeouts.implicit_wait = 5 # seconds

Alternatively, you can verify whether all the ads have loaded on the webpage either via JavaScript or jQuery command. Once complete, you can continue with test execution.

Nishant
  • 66
  • 4