WinAppDriver having issues identify elements in a driver when the driver changes too much - automation

For example: Launch Outlook on your desktop. Notice how there is a "splash loading screen" well the driver I have will look at this executable and wait x seconds before trying to click on the "New Email" button. However when it gets to the page where the new email button appears, it can't find it. Strange... hmm okay lets start the application but have it trigger the executable that is already in the process. It looks for the new email button and finds it no problem.
The only thing i can think of is that the driver loads the executable, the executable then changes its data drastically or something. Then all of a sudden i need to build a new driver. But I dont think this is the way to go about it.
[TestInitialize]
public void TestMethod1()
{
options.AddAdditionalCapability("app", #"C:\Program Files (x86)\<PATH>");
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), options);
System.Threading.Thread.Sleep(5000);
}
[TestMethod]
public void TEST()
{
LoginPage page = new LoginPage(new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), options)); // Notice how i am building a new driver just for this page. This is VERY heavy.
page.Login("USERNAME", "PASSWORD");
}

You probably have the "splash screen issue".
Here's a explantation about what to do about it.
In short, winappdriver creates a new windowhandle for every new window it encounters. As the splash screen counts for a separate window, you should have 2 windowhandles available at the moment you wanted to click that "new email" button. The most recent windowhandle will be the highest index.
You can switch between windowhandles like this (copied from link):
// Return all window handles associated with this process/application.
// At this point hopefully you have one to pick from. Otherwise you can
// simply iterate through them to identify the one you want.
var allWindowHandles = session.WindowHandles;
// Assuming you only have only one window entry in allWindowHandles and it is in fact the correct one,
// switch the session to that window as follows. You can repeat this logic with any top window with the sameĀ²
// process id (any entry of allWindowHandles)
session.SwitchTo().Window(allWindowHandles[0]);
Also take a look at this answer. Could be useful too.

Related

How to close popup windows in Selenium?

I'm doing a test with java and popup windows appear (ads) and I do not know how to close them to continue with my test.
I have to click on a button but I change the focus to the window.
The url is www.orbitz.com
Please help!
orbitz
Java solution:
//switch to opened tab
ArrayList<String> tabs_windows = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs_windows.get(1));
//close current tab and switch driver back to original
((JavascriptExecutor)driver).executeScript("close();");
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
I haven't tested this for a while, but in different browsers getWindowHandles() used to return different orders for the tabs. Not sure if you can assume 0 is the first tab for all browsers. Change the index accordingly, or store the current handle before popup, and close all that don't match that.

How to handle windows download popup for IE11 using Selenium Webdriver without using AutoIT

While testing on IE11, When I click on the link to download a doc, a new blank window opens up with save and Cancel option popup. I want to hit cancel, Switch back to my current window and continue validation.
Can anyone help me how to handle this without the use of AutoIT.
If you are using selenium-webdriver with Java then you can use Sikuli-api.
Just a small introduction: Sikuli is a tool which helps to achive automation task for any software(web/standalone). Base of Sikuli is a Screenshot of the control you would like to interact with.
In your case, it is just matter of 2 buttons. Hence I would not suggest you to use seperate autoIT generated *.exe file.
Just take screenshot of OK & Cancel button.
Showing you the sample code here:
import org.sikuli.script.*;
public class Test {
Screen m_screen;
SikuliScript m_sikscr;
#Test
public void Test1() throws FindFailed
{
m_screen=new Screen();
m_screen.wait((double) 10.0);
//Click on Cancel button
m_screen.click(new Pattern("./img/CancelButton.png"));
}
}
This much should do your task.
Please Note, if at all you decide to use this method, make sure that you handle all Sikuli related dependencies in java project.
Try manually at first whether by pressing escape key it dismissing the popup.
if so follow the below code,
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
r.keyPress(KeyEvent.VK_ESCAPE);

How to make Selenium WebDriver wait for page to load when new page is loaded via JS event

I'm working on automating a site which has a number of links which load new pages via a JS event. Basically, there are elements which are clickable, clicking on one causes some JavaScript to run and this leads to a form being submitted and routing to a new page.
Now if these were just standard HTML links there would be no problem as Selenium is smart enough to tell that there's a new page coming and to wait to do things. But as good as it is, Selenium can't tell that the clicks in this instance are leading to new pages to load so it doesn't wait and just keeps going. As such it doesn't wait for the new page, tries to find elements which aren't there and my tests all fail. Bummer.
As a temporary solution I'm just pausing the program for three seconds like so:
oldPageDriver.clickThatButton();
try {
Thread.sleep(3000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
newPageDriver = new NewPageDriver(driver);
newPageDriver.doStuffOnNewPage();
And this works, sort of. I don't like it because it's "hacky," and just interrupting the program instead of doing something smarter. Because the delay is hard coded at three seconds I get failures if the link is working but just slow. I've considered something like an implicit wait but that accomplishes the same thing and I've not found a solid, workable answer in Java anywhere after a considerable amount of looking.
So, can anyone suggest a way around this? Specifically, how to make Selenium know that a new page is expected and to wait until it's available?
The wait for the document.ready event is not the entire fix to this problem, because this code is still in a race condition: Sometimes this code is fired before the click event is processed so this directly returns, since the browser hasn't started loading the new page yet.
After some searching I found a post on Obay the testing goat, which has a solution for this problem. The c# code for that solution is something like this:
IWebElement page = null;
...
public void WaitForPageLoad()
{
if (page != null)
{
var waitForCurrentPageToStale = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
waitForCurrentPageToStale.Until(ExpectedConditions.StalenessOf(page));
}
var waitForDocumentReady = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
waitForDocumentReady.Until((wdriver) => (driver as IJavaScriptExecutor).ExecuteScript("return document.readyState").Equals("complete"));
page = driver.FindElement(By.TagName("html"));
}
`
I fire this method directly after the driver.navigate.gotourl, so that it gets a reference of the page as soon as possible. Have fun with it!
Explicit waits are what you need;
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
You can directly add this to your test or you may want to DRY it up, especially if there is a common wait expectation such as the disappearance of a spinning icon.
You could extend the click method to always wait after clicking or if following page objects, add a wait_until_loaded method to a base page class. There many other valid approaches but dependent on how the AUT is implemented
Simple ready2use snippet, working perfectly for me
static void waitForPageLoad(WebDriver wdriver) {
WebDriverWait wait = new WebDriverWait(wdriver, 60);
Predicate<WebDriver> pageLoaded = new Predicate<WebDriver>() {
#Override
public boolean apply(WebDriver input) {
return ((JavascriptExecutor) input).executeScript("return document.readyState").equals("complete");
}
};
wait.until(pageLoaded);
}

Selenium IDE window focus in Internet Explorer

I'm using selenium IDE and webdriver to test a web page in Internet Explorer. I discovered a while back that IE will not fully accept commands from Selenium if it's not the window in focus. For example, if the Selenium IDE window is in focus, and the command is to click a button in IE, the button will push down, but it won't let go.
With that in mind, my test involves popping up a window, doing a few things in it, leaving it open and returning to the null window to do a few things, then returning to the popup for a few more commands.
Is there a way I can make the null window come forward (over the popup) when I need to execute the commands for the null window? And then vice versa, can I make the popup then come forward when I need to return to it? I tried using windowFocus, but that did not work.
Use the SwitchTo() method and the TargetLocator Interface in Selenium.
A really simple example would look like this:
// Switch to new window
public String SwitchToNewWindow()
{
// Get the original window handle
String winHandleBefore = driver.getWindowHandle();
foreach(String winHandle in driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
return Constants.KEYWORD_PASS;
}
// Switch back to original window
public String switchwindowback()
{
String winHandleBefore = driver.getWindowHandle();
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)
return Constants.KEYWORD_PASS;
}
I remembered that webdriver sometimes acts differently than running non-webdriver tests. It turns out that using windowSelect followed by windowFocus switches between windows when running webdriver tests.

Click then CTRL Click not working with selenium-java 2.39 + Firefox 26

I'm facing this issue since some days now but didn't manage to overcome it trying different ideas.
Problem description: I wanna select a line in a table (GWT CellTable), perform some actions (which are my application specific) on it and then unselect back the line.
The line never gets unselected.
I'm quite new to selenium And I don't know if someone else has run into same problem and if there is a workaround to it. Thanks in advance
Code:
#Test
#SuppressWarnings("serial")
public void testClearEventCodes(){
refreshBrowser();
testWEHSearch();
WebContext faresContext = rootContext.gotoId(Strings.WEH_FARES_TABLE);
//INITIALLY HOT AND EVENT FARE
assertTrue("Y N N N N".equals(faresContext.gotoTableCell(1, 15).getText()));
assertTrue("CHINAYEAR".equals(faresContext.gotoTableCell(1, 16).getText()));
checkColorCodes(new HashMap<String, String[]>(){
{
put(getFareKey("GMP", "PAR", "KE", "0004", "K001", "OW", "Public"), new String[]{"1", COLOR_CODE_HOT_AND_EVENT_FARE});
}
});
faresContext.gotoTableRow(1).getElementWebContext(1).click();
rootContext.gotoId(Strings.WEH_CLEAR_EVENT_CODES_BUTTON).click();
faresContext.gotoTableRow(1).getElementWebContext(1).ctrlClick();
//ENSURE ALL EVENT CODES ARE CLEARED
assertTrue("".equals(faresContext.gotoTableCell(1, 16).getText()));
checkColorCodes(new HashMap<String, String[]>(){
{
put(getFareKey("GMP", "PAR", "KE", "0004", "K001", "OW", "Public"), new String[]{"1", COLOR_CODE_HOT_FARE});
}
});
}
And bellow is the method to CTRL CLICK the line:
/**
* Holds Control key and Clicks on current element.
*/
public void ctrlClick() {
Actions actionBuilder = new Actions(driver);
actionBuilder.keyDown(Keys.CONTROL).click(getSingleElement()).keyUp(Keys.CONTROL);
org.openqa.selenium.interactions.Action action = actionBuilder.build();
action.perform();
}
Your problem might be related to the new Firefox feature in which display settings are now taken into account. You can try changing the display settings for your computer to 100% and try again.
https://code.google.com/p/selenium/issues/detail?id=6774