David West
David West

Reputation: 2328

Watir Webdriver: testing that clicking outside a modal does not close it

Let's say I have a modal.

@my_modal = @brower.div(id: 'TheModal')

Let's say I have a test that brings up the modal (cucumber in this example)

 Given I ...
 When I ...
 Then "My Modal" is visible
 When I click outside the modal
 Then "My Modal" is visible

Now to define the step to click outside the modal:

 When /^I click outside the modal$/ do
   # what do I put here?
 end

Any ideas?

I just tried @browser.element(xpath: '//html').click based on How to simulate mouse click on blank area in website by Selenium IDE?. I got the error Selenium::WebDriver::Error::UnknownError: unknown error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.

Upvotes: 2

Views: 1581

Answers (2)

David West
David West

Reputation: 2328

I ended up with this @browser.driver.action.move_to(@browser.div(id: 'js-overlay').wd, 0, 0).click.perform.

There was a div in the background that dimmed everything, div#js-overlay. clicking on it normally with `@browser.div(id: 'js-overlay') didn't work b/c it clicked right in the middle where the modal was, so that's why there is that extra business for clicking the top left of that particular div (thanks to Justin Ko!).

Upvotes: 3

titusfortner
titusfortner

Reputation: 4194

Watir only allows you to click on elements that are visible. For most modal implementations, only elements inside the modal would be considered visible.

Try: @browser.element.wd.click

It could help to share the html if that does not work.

Upvotes: 3

Related Questions