Selenium race condition for dialog closing - selenium

I have a dialog opened. On trying to close it, the check for if dialog closed is timing out sometimes.
The dialog opens via transform: scale property of css. So, in the end I'm trying to check for scale(0), but its timing out on random basis.
self.wait().until(expected_conditions.visibility_of_element_located((By.XPATH, "//*[contains(#class, 'sample-dialog')][contains(#style, 'transform: scale(1)')]")))
self.selenium.find_element_by_class_name("sample-close-button").click()
self.wait().until(expected_conditions.visibility_of_element_located((By.XPATH, "//*[contains(#class, 'sample-dialog')][contains(#style, 'transform: scale(0)')]")))
The wait function is nothing but WebDriverWait defaulted to 10 secs, while the dialog takes merely 0.2s to close:
def wait(self, seconds=10):
return WebDriverWait(self.selenium, seconds)

Related

Verification of button's presence fails once button is clicked in Robot framework

In Robotframework, the 'Click Button' keyword do clicks the button, but when it comes to check that the button doesn't exist anymore and it's clicked, sometimes additional check ( like Page Should Not Contain or Element Should Not Be Visible ) Is fails or the script can't find the button to click it.
What I have tried:
For loop to click button- Code below
Wait Until Keyword - Code below
Click element using Javascript
Issue is intermittant fails 1 of 5 times.How can I deal with this Problem?
//Code Samples 2 different ways to click the button
1.
Wait Until Page Contains Element ${BTN_BET} 60s
Double Click Element ${BTN_BET}
FOR ${INDEX} IN RANGE 100
${CHECK}= Run Keyword And Return Status Page Should Contain Element ${BTN_BET}
Run Keyword If '${CHECK}' == "True" Double Click Element ${BTN_BET}
Run Keyword If '${CHECK}' == "False" Exit For Loop
END
Page Should Contain
... You clicked the button
2.
Wait Until Keyword Succeeds 60s 0.5s Page Should Contain Element ${BTN_BET}
Sleep 2s
Wait Until Keyword Succeeds 30s 0.5s Execute JavaScript document.evaluate
('${BTN_BET}',document.body,null,9,null).singleNodeValue.click()
Sleep 5s
Wait Until Page Contains Element //div[#class='results-warning' and .='Wait
for another stage'] 40s
Page Should Contain
... You clicked the button
So, I found out good decision that resolves my problem. I rewrote the FOR loop in that way
FOR ${INDEX} IN RANGE 20
${BET_BUTTON_NOT_PRESSED} = Run Keyword And Return Status Page Should Contain Element ${BTN_BET}
Run Keyword If '${BET_BUTTON_NOT_PRESSED}' == "True" Click Button ${BTN_BET}
Sleep 1s
Continue For Loop If ${BET_BUTTON_NOT_PRESSED} == "False"
END
The problem was in the button, it did not disappear immediately, but when I added 'Sleep 1s' in my loop - 10 of 10 tries was passed. I think that the problem itself is javascript is loading too slow.

Element locator not found

I use Selenium2Library with Robotframework. With my code source, I go to a web browser and after 2 windows open. Then I close one window after I try to click on an element locator on the window but the element isn't found. I have error : 'id=ggroup1Mon' did not match any elements after 5 seconds.
Open Browser http://10.70.150.34/avldisplay/MONITORING/Authentification
Wait Until Element Is Visible id=j_username
Click Element id=j_username
Input Text id=j_username 452
Wait Until Element Is Visible id=j_password
Click Element id=j_password
Input Text id=j_password 463425
Wait Until Element Is Visible xpath=//button[#type='submit']
Click Element xpath=//button[#type='submit']
${az}= List Windows
Select Window #{az}[1]
Close Window
Select Window #{az}[0]
Wait Until Element Is Visible id=ggroup1Mon
#Click Element id=ggroup1Mon

How to Handle the multiple DropDowns in selenium(Country/State/City) --Selenium WebDriver Data Driven Frame Work

I have written a selenium data driven code, which will fetch data from the excel file and will ,fill up in the web page.
the Webpage contains
drop downs for Country ,State and City,
Text Field 1 and Text Field 2
Add/update Button and Clear Button
Steps
1-select country,state,city ,enter text field 1 and Add/Update button.
Issue
When I perform above action, when Add/update is clicked ,I want page to wait until again starting the loop ,so that the country drop downs resets again.
as of now it is not waiting and causing even a new country is selected the state drop-down shows previously selected country's provinces.Example(If I select India and provinces for the 1st record, than next time for United states the "State-drop down" displays ,Indian States but not the United States"
I tried using wait until technique even the driver.manage().timeouts().implicitlyWait(20, TimeUnit.MILLISECONDS), even thread.currentthread.sleep
but none of them are consistent as after some records I am getting error as element not found because of the above mentioned issue.
I want post Add/update button is clicked the driver to wait till it again resets the page with default value of the country set Secondly when I pass a value for the country the driver again to wait so that it can load it corresponding States .
I hope this could be handled in this way:
Step 1: Write small method where in you send a Webelement and the desired value that you need to select.
Step 2: In that method, before selecting a value from drop down, read the value from it and check for the desired value you looking for.
Step 3: If you don't find the desired value then wait for say 'x' seconds and then loop it again for desired value that you are looking for.
Step 4: Once you get desired value, select it and exit the loop.
By this above method, you can handle as many combo boxes as you like.
For reference: how to read values from combo box
In my opinion you should use some intelligent wait condition here so that it waits until the element gets activated or enabled.
Select any value from first drop down
Wait until page to be loaded completely
Wait until second drop down gets activated
Select any value from second drop down
Wait until page to be loaded completely
Wait until second drop down gets activated
If above solution doesn't work then go for 'refresh', code should be able to refresh the page before selecting any value from first drop down.
// Code to wait until page get completely loaded
public void waitUntilPageIsLoaded() {
ExpectedCondition<Boolean> condition = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
};
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(condition);
}

Using only one NSThread for one method

I have refresh button in my app and method 'doRefresh' who run in background thread. Now if user press double click - method doRefresh run 2 times and more.
I want if user press double click and more clicks, all previos threads stop work and complete. And new method begin run in new thread. How I to do this?
More details: I work with CoreData, and parse long XML file for fill relationship objects. When user press on refresh button 10 times for example, hi catch next error:
reason: '*** Collection <__NSCFSet: 0x5b7dd50> was mutated while being enumerated.<CFBasicHash 0x5b7dd50 [0x1503400]>{type = mutable set, count = 8
Here's how things may work
1. Keep a reference to last thread spawned
2. In each thread spawned keep a variable, say var_cancel
3. When a new thread is created, set the var_cancel to true for previous thread.
4. In the thread, check for var_cancel at short durations. If found true, exit the thread.
5. Make sure the thread does not produce any side effects between var_cancel set true to exit of thread.

Display time left when running

I have a form with a few buttons which execute code when pressed like running validations on the database.
Some code can run for a few minutes so is there any way to show the time remaining or a message to display the % of process completed?
Or pop out a message when code evaluation starts and the message should disappear once code running is completed?
What you are probably looking for is a "Progress Bar".
I've used the Microsoft ProgressBar control (you can find it under Insert->ActiveX Control), and it's not that hard to use. Just set the value of it to a percentage (as an integer, not a decimal).
'foo, being the ProgressBar
me.foo = 70 '70%
There is some good info here on another method: http://www.granite.ab.ca/access/progressbar.htm
In order to do this the "normal" way, you'd need to run your validation in another thread and have it report its progress back to the UI thread. However, I don't believe VBA supports any kind of multithreading.
If your validation routines involve a loop, or even just many separate discrete operations, you can try inserting a DoEvents statement in between loop iterations (or operations), and then have your progress display updated periodically (say, in an Application_OnTime event handler).
I usually have a form I name frmProgress or whatever, with a cancel button and a label for displaying a status message. Then embedded in the form code I have a boolean called bCancel, and when you hit the cancel button it simply sets bCancel as true.
Also in this code I have a routine called ShowPercDone( Idx , NumIdc ) where Idx is the step the code is on, and NumIdc is the number of steps the code will take (assuming each step takes the same amount of time). This works well when I'm running through a for loop, but basically any time I want to display a status update I just call the routine in the form with my message, which I should add runs the doevents command for me.
So that's how the status form works. In the macro I run, I start out by just calling frmProgress.show (0) so that it lets you click the cancel button. Then in my loop when I update the status message I then check frmProgress.bCancel and if it's true I exit out of the macro.
Hope that helps.
Finally to be simple i decided to use the method given here
http://oreilly.com/pub/h/3330#code