ControlID changes each time a window is launched - autoit

Consider this AutoIt code:
WinActivate("Test Window")
Sleep(5000)
ControlClick("Test Window", "", 197128)
There is a button in the window I am testing. I have written a simple script to click on the button using ControlClick. I use controlID to identify and click on the button (I use AutoIt window Info to get the controlID). The script works. The problem is that the control ID changes each time the test window is launched. As a result, the script does not simulate a button click each time a new instance of the window is launched. How can I make controlclick work as expected every time?

For controls that have dynamic control Ids, the best is to use advanced detection.
As it can be seen in the Help File:
A special description can be used as the controlID parameter used in most of the
Control...() functions. This description can be used to identify a control by the
following properties:
For example,
ControlSend("Untitled - Notepad", "", "[CLASS:Edit; INSTANCE:1]", "This is some text")
Or
ControlClick("My Window", "", "[CLASS:Button; TEXT:Finish; INSTANCE:2]")
Don't forget to set:
Opt("WinTitleMatchMode", 4) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

Related

How to close the popup window which appears during file downloads

In my implementation of the IDownloadHandler interface i have the following in OnDownloadUpdated. The window stays open??
If downloadItem.IsComplete Or downloadItem.IsCancelled Then
browser.CloseBrowser(True)
browser.Dispose()
End If
Must I have this popup window when a download is happening? I have read here I do, if yes how do I close it. I really thought I read in the project issues/comments that the default was now to automatically close it.
Following the comment of amaitland (Thank you) I have my new window closing properly.
For the VB people - here is what I have added to the OnDownloadUpdated event
If downloadItem.IsInProgress = False And browser.IsPopup And browser.HasDocument = False Then
browser.GetHost.CloseBrowser(False)
browser.GetHost.Dispose()
End If

What is the most efficient way of filling in details on a web form?

Take a standard web page with lots of text fields, drop downs etc.
What is the most efficient way in webdriver to fill out the values and then verify if the values have been entered correctly.
You only have to test that the values are entered correctly if you have some javascript validation or other magic happening at your input fields. You don't want to test that webdriver/selenium works correctly.
There are various ways, depending if you want to use webdriver or selenium. Here is a potpourri of the stuff I'm using.
Assert.assertEquals("input field must be empty", "", selenium.getValue("name=model.query"));
driver.findElement(By.name("model.query")).sendKeys("Testinput");
//here you have to wait for javascript to finish. E.g wait for a css Class or id to appear
Assert.assertEquals("Testinput", selenium.getValue("name=model.query"));
With webdriver only:
WebElement inputElement = driver.findElement(By.id("input_field_1"));
inputElement.clear();
inputElement.sendKeys("12");
//here you have to wait for javascript to finish. E.g wait for a css Class or id to appear
Assert.assertEquals("12", inputElement.getAttribute("value"));
Hopefully, the results of filling out your form are visible to the user in some manner. So you could think along these BDD-esque lines:
When I create a new movie
Then I should see my movie page
That is, your "new movie" steps would do the field entry & submit. And your "Then" would assert that the movie shows up with your entered data.
element = driver.find_element(:id, "movie_title")
element.send_keys 'The Good, the Bad, the Ugly'
# etc.
driver.find_element(:id, "submit").click
I'm just dabbling in this now, but this is what I came up with so far. It certainly seems more verbose than something like Capybara:
fill_in 'movie_title', :with => 'The Good, the Bad, the Ugly'
Hope this helps.

catching change in spinbox - rtcltk

I'm creating a spinbox in R using rtcltk with:
from <- tkwidget(leftFrame, type="spinbox", from=0, to=0.1,
inc=0.001, textvariable=freqFrom,
command = function(){updatePlot()})
This works as intended (updatePlot is called) when I use the arrows of the spinbox, but does not work if I just type something in manually.
How do I catch the "value changed" event?
By default it does not change in this case in case you type in an illegle value (like deleting the last digit), or if the update is time consuming then you would not want it to update between every keystroke when typing in a 3 or 4 digit number.
You can add an update button than calls updatePlot when clicked so that the user would type in the number and when they know they are finished would click the button.
If you really want the update to occur with every keystroke then you can use the tkbind function to call updatePlot (something like tkbind(*spinbox*, "<Key>", updatePlot) where spinbox is the variable pointing to the spinbox).

How to get texts in ListBox using AutoIt

I am using AutoIt to create an auto-install application. There is an dialog which contains a ListBox control, and in the listbox there are some choices for user (the detailed choices depends on user's machine. For some users there maybe only one choice, for some users there may be three choices, etc.), so I want to get the texts in the listbox to make the decision. I have tried the following code, but it did not work.
; 2223 is the ID of listbox
$txt = ControlGetText("Select Web Site", "", "[ID:2223]")
Msgbox(0, "", $txt)
After execution $txt is null.
So what should I do to get the texts in Listbox?
Here is the attribute of the listbox monitored by AutoIt v3 Window Info:
Class: WindowsForms10.Listbox.app.0.33c0d9d
I've found the 'Send' command to be unreliable on occasion, particularly if the PC is locked.
'ControlSend' has always worked to get the keystrokes where I want them.
I wrote a test to check to see if an item was in a combo box. There might be similar functions for list boxes using GuiComboBox.au3.
Func DoesItemExistInComboBox($windowtitle, $windowtext, $comboboxcontrol, $itemtocheck)
$returnvalue = 0
$ComboBoxHandle = ControlGetHandle($windowtitle, $windowtext, $comboboxcontrol)
$ComboBoxArray = _GUICtrlComboBox_GetListArray($ComboBoxHandle)
For $i = 0 TO UBound($ComboBoxArray)-1
If $ComboBoxArray[$i] = $itemtocheck Then
$returnvalue = 1
EndIf
Next
return $returnvalue
EndFunc
What about:
ControlCommand("My GUI", "", "[CLASS:ListBox; INSTANCE:1]", "SelectString", "item2")
What I want to do is to select one of the items named "Default Web Site" in the list, but it seems that the list content can not be got, so finally I tried another way:
At first I make the listbox focused, and then I choose the item "Default Web Site" by sending "Def":
ControlFocus($Title, "", "[NAME:lbWebSites]")
; Select the option "Default Web Site", so press "def" to set the desired item.
Send("Def")

How to test a confirm dialog with Cucumber?

I am using Ruby on Rails with Cucumber and Capybara.
How would I go about testing a simple confirm command ("Are you sure?")?
Also, where could I find further documentation on this issue?
The selenium driver now supports this
From Capybara you would access it like this:
page.driver.browser.switch_to.alert.accept
or
page.driver.browser.switch_to.alert.dismiss
or
page.driver.browser.switch_to.alert.text
Seems like there's no way to do it in Capybara, unfortunately. But if you're running your tests with the Selenium driver (and probably other drivers that support JavaScript), you can hack it. Just before performing the action that would bring up the confirm dialog, override the confirm method to always return true. That way the dialog will never be displayed, and your tests can continue as if the user had pressed the OK button. If you want to simulate the reverse, simply change it to return false.
page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')
I've implemented these two web steps in /features/step_definitions/web_steps.rb:
When /^I confirm popup$/ do
page.driver.browser.switch_to.alert.accept
end
When /^I dismiss popup$/ do
page.driver.browser.switch_to.alert.dismiss
end
If you want to specifically test the message being displayed, here's a particularly hacky way to do so. I don't endorse it as beautiful code, but it gets the job done. You'll need to load http://plugins.jquery.com/node/1386/release, or change it to do cookies natively if you don't want jQuery.
Use this sort of story:
Given I am on the menu page for the current booking
And a confirmation box saying "The menu is £3.50 over budget. Click Ok to confirm anyway, or Cancel if you want to make changes." should pop up
And I want to click "Ok"
When I press "Confirm menu"
Then the confirmation box should have been displayed
And these steps
Given /^a confirmation box saying "([^"]*)" should pop up$/ do |message|
#expected_message = message
end
Given /^I want to click "([^"]*)"$/ do |option|
retval = (option == "Ok") ? "true" : "false"
page.evaluate_script("window.confirm = function (msg) {
$.cookie('confirm_message', msg)
return #{retval}
}")
end
Then /^the confirmation box should have been displayed$/ do
page.evaluate_script("$.cookie('confirm_message')").should_not be_nil
page.evaluate_script("$.cookie('confirm_message')").should eq(#expected_message)
page.evaluate_script("$.cookie('confirm_message', null)")
end
Updating this for current releases of Capybara. Most Capybara drivers today support the modal API. To accept a confirm modal you would do
accept_confirm do # dismiss_confirm if not accepting
click_link 'delete' # whatever action triggers the modal to appear
end
This can be used in Cucumber with something like
When /^(?:|I )press "([^"]*)" and confirm "([^"]*)"$/ do |button, msg|
accept_confirm msg do
click_button(button)
end
end
which will click the named button and then accept a confirm box with text matching msg
The capybara-webkit driver supports this as well.
Scenario: Illustrate an example has dialog confirm with text
#
When I confirm the browser dialog with tile "Are you sure?"
#
=====================================================================
my step definition here:
And(/^I confirm the browser dialog with title "([^"]*)"$/) do |title|
if page.driver.class == Capybara::Selenium::Driver
page.driver.browser.switch_to.alert.text.should eq(title)
page.driver.browser.switch_to.alert.accept
elsif page.driver.class == Capybara::Webkit::Driver
sleep 1 # prevent test from failing by waiting for popup
page.driver.browser.confirm_messages.should eq(title)
page.driver.browser.accept_js_confirms
else
raise "Unsupported driver"
end
end
Prickle adds some handy convenience methods for working with popups in selenium and webkit
This gist has steps to test a JS confirm dialog in Rails 2 and 3 with any Capybara driver.
It's an adaptation of a previous answer, but doesn't need the jQuery Cookie plugin.
Tried the above answers with no luck. In the end this worked for me:
#browser.alert.ok

Resources