How I can access the data from a popup page with watir? - ruby

I want to access the data in a popup window containing a login form, this window opens when you click on a logo, this line contains the code to execute the javascript: <div id="loadDIV" class="containerLogo" onclick="ventanaSecundaria('/Security-war/autentia/Login.jsp?sion=c5ac379249711c18cf6e030c8400',800,600); "></div>
with this line run the script:
browser = browser.div(:class => "containerLogo").fire_event :click
watir open in a new popup form.
The problem is that I can not access the data from the popup window and need to fill the form containing, try the following:puts browser.text.include? 'Login'
and get false, then I thought that maybe I could not access the other popup and look for something to not open another window but only refresh the I'm using and so we can access its contents, looking I found that I can do this:
browser.goto("javascript:document.getElementByClass()'containerLogo').click()")
but I try to console returns 'true' and the browser does not update.
Update
You can switch windows simply with this:
browser.window(:index => 1).use
and to be sure we change verify the url window with this:
browser.window.url

When a new browser window is opened, you can then ‘use’ the new
window. [Browser Popups | Watir Webdriver]
browser.window(:title => "annoying popup").use do
browser.button(:id => "close").click
end

Related

Open new window when already logged with watir

Is there a way to open a new window or tab when already logged in a site, and open it as a logged user?
When I try to open it with a new browser window it opens it as a guest user.
My code structure simplified looks like this right now:
b.goto url
Login(b)
link = b."getlink"
new_b.goto link
I want new_b to open as logged.

Code to navigate filepicker with watir?

I am testing an application that uses filepicker for file uploads:
https://www.inkfilepicker.com
I am using watir.
I have tried:
def upload_photos
$b.link(:text, "Upload Photos").click
$b.button(:text, "Choose File").click
end
but the code fails with:
`assert_exists': unable to locate element, using {:text=>"Choose File", :tag_name=>"button"} (Watir::Exception::UnknownObjectException
Is it possible to automate filepicker uploads with watir? How?
The code
$b.button(:text, "Choose File").click
Has two problems (assuming your filepicker is the same as that on the inkfilepicker demo page):
The Choose File button is in an iframe. When it comes to frames, you need to explicitly tell Watir about them.
The Choose File is not a regular button; it is the button for a file field (). These are accessed from Watir using the file_field method. There is no support for just clicking the button. Instead, there is a set method that will click the button, select a file to upload and close the window.
Assuming the filepicker in your application is the same as the one on the inkfilepicker demo page, you can do the following:
require 'watir-webdriver'
browser = Watir::Browser.new :firefox
# File to upload
file = 'C:\Users\user\Desktop\stuff.jpeg'
# Go to the demo page, which has a file uploader
browser.goto 'https://www.inkfilepicker.com/demos/'
# Click the button that opens the file uploader
browser.button(:class => 'zip-open-button').click
# Wait for the dialog to be displayed
browser.div(:id => 'filepicker_dialog_container').wait_until_present
# Set the file
browser.frame(:id => 'filepicker_dialog').file_field(:id => 'fileUploadInput').set(file)

Selenium Webdriver with Javascript Asynchronus Popup

I am buidling a test script for a webpage which posts data to some application and shows the result back. The problem is that the script does not load the data from the javascript modal popup. I would like to state here that it is not an alert it is a simple javascript popup.
The popup has a link in it like
Google
The script click a link and then the popup appears but it can not read the link from the popup. With a lot of testing I have came to the conclusion that the script runs through the page before the popup appears. This is the reason that it does not read the popup link. Follow code is being used to read popup.
#driver.find_element(:css, "a.popup-link").attribute("href")
I have also tried both implicit and explicit waits but still the problem persists. I have set the implicit wait to 30 seconds by using
#driver.manage.timeouts.implicit_wait = 30
The explicit wait like this
wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
wait.until { driver.find_element(:css => "a.popup-link") }
element = #driver.find_element(:css, "a.popup-link")
What should i do so that i can read the popup link properly? Any suggestions?

Suppress auto-closing window in Watir

I am using Watir with Chromedriver to automate form submission on some website. I have to login and submit multiple forms. The problem is, when I click the submit button the page the page automatically closes, so when I goto('next_url') I get this error:
/Users/jackz/.rvm/gems/ruby-1.9.3-p327/gems/selenium-webdriver-2.27.2/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok': 'auto_id' does not refer to an open tab (Selenium::WebDriver::Error::UnknownError)
The Watir instance is still there, but the window is closed. I could create a new instance every time, but then I would have to login again every time and this would take longer.
So how can I either:
Open a new window in the same Watir instance
or
Suppress the window from closing after I submit
require 'watir-webdriver'
#b = Watir::Browser.new :chrome
#b.goto(URL)
#b.buttons.first.click
#this is when the window closes
#b.goto(NEW_URL)
#then I get an error
Thanks
I figured out an answer to my own question. I can open a new window in Watir using javascript:
b = Watir::Browser.new
b.execute_script("window.open()")
b.windows.last.use
This opens a window where I can fill out the form, then when the window automatically closes I still have the original window to work with. Probably not the best solution, but it works for now.
Add this to your existing code if your using chrome. else, modify accordingly as per your browser.
This will keep the window open, and the current watir session active.
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {'detach' => false})
browser = Watir::Browser.new :chrome
What Mrityunjeyan suggested above was in a right direction, but you need to change a few things to make it work.
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {'detach' => true })
b = Watir::Browser.new('chrome', desired_capabilities: caps)
Check the documentation here.
https://sites.google.com/a/chromium.org/chromedriver/capabilities

Wicket - Internet Explorer double submit

I have a big problem with Internet Explorer 7 and 8.
SITUATION:
I have a FORM that build a Medical Prescription. When I hit the save button, the script saves the DomainObject on DB and set a boolean property (of panel where the form is added) called "saved" to true and a byte[] property called PDF with bytestream.
On RenderHead of Panel, I read this boolean and, if is true, I force the trigger of a hidden button with this code:
String js = "$('#" + printPDF.getMarkupId() + "').click();";
response.renderOnDomReadyJavaScript(js);
The button executes this code:
ResourceStreamRequestHandler handler = new ResourceStreamRequestHandler(new ByteArrayResourceStream(pdf, "application/pdf"));
handler.setFileName("foo.pdf");
RequestCycle.get().scheduleRequestHandlerAfterCurrent(handler);
This code work perfecly on FF and Chrome. The Browser download windows appears and the user can save the PDF on HD.
Unfortunally, Internet Explorer has that damn security behavior that is triggered when a site require something to download. That warning require a user validation. A yellow Bar appear and the user is force to hit "Download".
screenshot http://imageshack.us/a/img198/1438/securityg.jpg
When I hit Download File, the form is submitted again with the exact state I had when I hit save the first time. So no previous INSERT on DB is already committed; The Session is resetted to the previous state etc...
The result is a double INSERT on DB of the Domain Ojbect.
Any clue to resolve this?
The problem is that you click download link programaticly instead to redirect browser to an URL or open an URL by JS window.open(url). Click a link looks like an unwanted operation that is sometimes restricted by browser.

Resources