Intermittent failure of Sikuli - automation

I have integrated Sikuli with my Selenium project. For the sake of learning, I have used simple gmail login application to automate it using Sikuli. Well, I am able to execute script. Now let's say, I'm typing something in my Username field. And sometimes, the mouse is not hovered to the username field. So my test scripts failed. And it is intermittent behavior.
public static void main(String[] args) throws Exception {
Screen screen = new Screen();
Pattern pattern1 = new Pattern("E:\\Projects\\Java\\Demo\\Images\\UserName.PNG");
Pattern pattern2 = new Pattern("E:\\Projects\\Java\\Demo\\Images\\Password.PNG");
Pattern pattern3 = new Pattern("E:\\Projects\\Java\\Demo\\Images\\SignIn.PNG");
Pattern pattern4 = new Pattern("E:\\Projects\\Java\\Demo\\Images\\Next.PNG");
Pattern pattern5 = new Pattern("E:\\Projects\\Java\\Demo\\Images\\SignedIn.PNG");
Pattern pattern6 = new Pattern("E:\\Projects\\Java\\Demo\\Images\\SentMail.PNG");
Pattern pattern7 = new Pattern("E:\\Projects\\Java\\Demo\\Images\\SentMessage.PNG");
System.setProperty("webdriver.chrome.driver","E:\\Projects\\Java\\Demo\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.navigate().to("https://www.gmail.com");
driver.manage().window().maximize();
screen.type(pattern1,"email id");
screen.click(pattern4);
screen.type(pattern2,"password");
screen.click(pattern5);
screen.click(pattern3);
screen.wait(pattern6,20);
screen.click(pattern6);
screen.wait(pattern7,5);
screen.click(pattern7);
}
Does anyone have an idea why this happens?

First of all, share your code.
Usually, intermittent behavior like you describe is caused by timeouts. Meaning that you are looking for an element that is not there yet or has not yet become stable.
A practical example in your scenario can be trying to detect the username field before the page has fully loaded. It will be useful to know how you have used both tools. What you used for navigation and what for elements identification?
Saying that, the quickest way to try and solve this problem is to put few seconds delay before you start searching for username element. See if that helps you.
EDIT
Now when you have posted your code, have a look at these two lines:
driver.manage().window().maximize();
screen.type(pattern1,"email id");
Here, you maximize the browser window and immediately try to find and type into the element described by pattern1. This is likely to be a problem since your driver instance does not wait for the window to become maximized and the next command will start executing immediately. You should allow some time to ensure that the window has finished resizing. just add a short sleep between these lines and see if that helps.

As it happens intermittently and occurs for the very first action in a newly drawn screen this looks like a timing problem.
The Sikuli solution here is to wait until your input field is available before you use it.
The statement to use is:
wait(pattern1[, seconds])
Insert just before:
screen.type(pattern1,"email id");
Reference:
http://doc.sikuli.org/region.html#Region.wait

Related

Trying to automate a web page and pause in debugger error [duplicate]

Everytime I try to access this website and open google-chrome-devtools I am unable to inspect any of the elements through the Inspector as the UI is having an overlay along with a message Paused in debugger.
The upvoted and accepted answer of this discussion says to check the Source tab, check under the Event Listener Breakpoints panel if you've set any breakpoints under 'Mouse'. I have cross checked that none of the Sources -> EventListenerBreakpoint are set.
The upvoted and accepted answer of this discussion says to check if the little octagonal stop/pause sign (at lower left of Chrome "Sources") is colored (blue or purple). I am not sure why do I need to do that additionally for selected websites.
Snapshot:
The upvoted and accepted answer of this discussion speaks about the Manual Steps.
All the solutions seem to point towards the manual process. But this issue seems to me the root cause behind Selenium being unable to getPageSource().
Code trials:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
Output: Chrome opens but doesn't navigates to the url.
So my questions are:
In which case can Paused in debugger error occurs?
Is it an error from the frontend development?
How can I bypass this error during the Automated Tests through Selenium?
In which cases can the Paused in debugger error occur?
Anytime you are accessing this page with the dev tools open. The debugger; line will pause javascript execution, but browsers will ignore it if the dev tools are closed.
Is it an error from the frontend development?
In this case, no--they're deliberately trying to keep you out. The purpose of this function is to pause execution and then redirect your browser to a different page if it takes longer than 100ms to resume. I would speculate that this is designed to interfere with automated crawlers like selenium, because a regular user wouldn't be affected and a human developer can just hack around it.
How can I bypass this error during the Automated Tests through Selenium?
My first recommendation would be to try running Selenium headlessly, if that's an option. If not, use the hotkey to resume execution (F8). You can use whatever method you like for generating a keypress; with the java.awt package it will look something like this:
Robot robot = null;
try
{
robot = new Robot();
}
catch(Exception e)
{
//handle failure
}
robot.keyPress(KeyEvent.VK_F8);
Remember that you have to trigger this within 100ms, so use whatever logic you like to detect the block and respond quickly. If you just want something quick and dirty, I would just make it spam F8 keypresses every 50ms for a period of time until you're certain the page has loaded.
EDIT: On further investigation, this page is extremely messy and hostile to anyone with the dev tools open. There is not one but several functions that trigger debugger;and they get called repeatedly on a timer for as long as you're on the page. Running headlessly seems like the best choice, unless you want to continue spamming F8 for the entire session.

Debugger.setPauseOnExceptions for Selenium debugging not working [duplicate]

Everytime I try to access this website and open google-chrome-devtools I am unable to inspect any of the elements through the Inspector as the UI is having an overlay along with a message Paused in debugger.
The upvoted and accepted answer of this discussion says to check the Source tab, check under the Event Listener Breakpoints panel if you've set any breakpoints under 'Mouse'. I have cross checked that none of the Sources -> EventListenerBreakpoint are set.
The upvoted and accepted answer of this discussion says to check if the little octagonal stop/pause sign (at lower left of Chrome "Sources") is colored (blue or purple). I am not sure why do I need to do that additionally for selected websites.
Snapshot:
The upvoted and accepted answer of this discussion speaks about the Manual Steps.
All the solutions seem to point towards the manual process. But this issue seems to me the root cause behind Selenium being unable to getPageSource().
Code trials:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
Output: Chrome opens but doesn't navigates to the url.
So my questions are:
In which case can Paused in debugger error occurs?
Is it an error from the frontend development?
How can I bypass this error during the Automated Tests through Selenium?
In which cases can the Paused in debugger error occur?
Anytime you are accessing this page with the dev tools open. The debugger; line will pause javascript execution, but browsers will ignore it if the dev tools are closed.
Is it an error from the frontend development?
In this case, no--they're deliberately trying to keep you out. The purpose of this function is to pause execution and then redirect your browser to a different page if it takes longer than 100ms to resume. I would speculate that this is designed to interfere with automated crawlers like selenium, because a regular user wouldn't be affected and a human developer can just hack around it.
How can I bypass this error during the Automated Tests through Selenium?
My first recommendation would be to try running Selenium headlessly, if that's an option. If not, use the hotkey to resume execution (F8). You can use whatever method you like for generating a keypress; with the java.awt package it will look something like this:
Robot robot = null;
try
{
robot = new Robot();
}
catch(Exception e)
{
//handle failure
}
robot.keyPress(KeyEvent.VK_F8);
Remember that you have to trigger this within 100ms, so use whatever logic you like to detect the block and respond quickly. If you just want something quick and dirty, I would just make it spam F8 keypresses every 50ms for a period of time until you're certain the page has loaded.
EDIT: On further investigation, this page is extremely messy and hostile to anyone with the dev tools open. There is not one but several functions that trigger debugger;and they get called repeatedly on a timer for as long as you're on the page. Running headlessly seems like the best choice, unless you want to continue spamming F8 for the entire session.

Selenium:how to create test evidence report with NUnit

I was wondering how one would create proper test evidence of Selenium UI tests.
I was thinking about screenshots but they do not actually cover everything you have done as it is difficult to determine when to screenshot. (Every click or every wait or every pageload).
Another option I thought about was screen recording, but this makes it rather difficult to work with parallelism as you record the whole screen and not a specific chrome window.
However, you could possibly also take a screenshot every second via the webdriver and turn this into a video. Then one would have to work with a separate thread which can be quite challenging considering the condition you have to provide to stop the thread from taking screenshots. Else the test will run forever.
As I was not able to draw a convincing conclusion based on my own thoughts about creating a test evidence report for UI tests I was hoping someone could explain to me how to properly do this.
I had similar issue and I've introduced in my automation framework ExtentReports + klov server with Testrail as tool for test-management.
I think that nobody would ask of You to show testcases via video or screenshot, but if that is necessary, You can check out several libraries for taking 'video', because this is not actual video, than rather bunch of screenshots that are mashed into one video.
What has actually proven really good investment of time is to take fallen tests cases screenshot and attach it in testcase result (Testrail, Bugzila, Extentreports whatever).
Actualy if using selenium/appium You can check this repo
[https://github.com/groupon/Selenium-Grid-Extras] they make 'video' like mentioned and stored on local hub/node.
But best menthod that has been real good method was report with detail steps of each testcase:
Screenshot testcase with detailed steps and action:
The way I handled the reporting in my application goes in the line of what Kovacic said.
I also used ExtentReports as a way to generate metrics and having a step by step record of what happened.
I created a method reponsible for recording a step ( clicked that, navigated there, asserting that... ) with the option of taking a screenshot if needed, and another one for starting a new test.
Then , it's a matter of calling those methods in the PageObject style testing framework and pretty much having those method called in every action made by your framework.
To better illustrate here are some implementation examples (c#) :
Log a step method
public void LogStep(Status status,string MessageToLog, bool hasScreenshot)
{
//we leave the possibility of taking the screenshot with the step or not
if (hasScreenshot)
{
Test.Log(logstatus, messageToLog)
.AddScreenCaptureFromPath(GetScreenshot());
}
else
Test.Log(logstatus, messageToLog);
}
Screenshot capture method
public static string GetScreenshot()
{
ITakesScreenshot ts;
//Browser.Driver here is the instance of the Driver you want to take screenshots with
ts = (ITakesScreenshot)Browser.Driver;
var screenshot = ts.GetScreenshot();
// Here just input the name you want your screenshot to have, with path
var screenshotPath = ScreenShotFolder + #"\" + _screenshotcount + ".bmp";
screenshot.SaveAsFile(screenshotPath);
// I've introduced a variable to keep track of the screenshot count (optional)
return (ScreenShotFolder.Substring(_reportRoot.Length) +"/"+ _screenshotcount + ".bmp");
}
Example of call in the framework
public void BlockAccount()
{
try
{
_blockAccBtn.Click();
_confirmBtn.Click();
ExtentReportGenerator.LogStep(Status.Info, "Blocking Account");
}
catch (NoSuchElementException)
{
ExtentReportGenerator.LogStep(Status.Fail, "Could not find block button", true);
}
}
A NunitTest using the whole system
[TestCase, Order(1)]
public void CanBlockCard()
{
//Creates a new test in the report
ExtentReportGenerator.Test = ExtentReportGenerator.Extent.CreateTest(GetCurrentMethod());
//Each one of these calls to the framework has logged steps
CashlessPages.CashlessAffiliationsPage.AccessAccount(1, 1);
CashlessPages.CashlessAccountsPage.BlockAccount();
Assert.IsTrue(CashlessPages.CashlessAccountsPage.IsAccBlocked());
}
Example of generated Report
Hope this helps

Webdriver/Selenium Alert window issue

My company wants me to develop a "Visual" GUI style BDD function using JBehave and Selenium, which uses javascript alert/confirm popup window to prompt user what is the exact step the running test reaches, eg:
Given I goto "www.google.com"
When I login
So we want to add Javascript alert window to popup during the automation test, the popped up window has the "OK" button, so when user click the OK button, the test will continue to the next step, and so on...
My issue is: I wrote a javascript func using Selenium's executeScript API which invoke the pop up alert window:
public void stepText(String step) {
executeScript("alert('"+step+"');");
}
So I expect when I click the OK button, the popped up window will disappear and test will continue to next step... But what shocked me is that when I click it, the test throw exception and crashed...
The exception is: selenium.WebDriverException
But I found if I add the following code to make the test automatically detect the alert window and accept it by using the following usual selenium alert handle function:
Alert alert=switchTo().alert();
alert.accept();
This can make the test runs well, so it looks I can NOT manually click the alert (after I manually click, the selenium still can NOT go back to the browser...lost connection to browser?), but the automation alert handle code works...
Of course, we want to let user to manually to click alert window to control the test execution, not the automation handle alert.
I really got stuck here for a while, and did a lot googling to search, but can not find similar example online, I hope you can shed me light on it, since you are much more guru than me on JBehave and Selenium.
I will be much grateful if you can help me out.
Selenium is a browser automation tool, it does not anticipate user's interactions.
Therefore, I'd use a simple Java GUI window to present the user with messages/options. Afterall, you are testing a web application in a browser, but the program itself is Java and has nothing to do with the browser. A usual Swing option dialog should be enough.
JOptionPane.showMessageDialog(null, "Login successful.");
String loginAs = JOptionPane.showInputDialog("Login as:", "admin");
int choice = JOptionPane.showConfirmDialog(null, "Use production data?");
(note that you don't want to invoke this in the EventQueue.invokeLater() block, because you want the dialogs to be blocking)
This way, you won't interact with Selenium or the browser in any way, you won't confuse it and you'll get the user input cleanly.
That said, if you insist on using alerts, I think it's definitely doable, but as of now (June 2013, Selenium 2.33.0), I don't know how:
The issue is not reproducible on IE8. After the executeScript("alert('Something.')"); call, Selenium waits for the call to return something and then proceeds normally. So you're good on IE.
However, with FF21, Selenium fails immediatelly with UnhandledAlertException just as you said.
I tried two obvious solutions:
js.executeScript("alert('something')");
new WebDriverWait(driver, 10)
.pollingEvery(100, TimeUnit.MILLISECONDS)
.ignoring(UnhandledAlertException.class)
.until(ExpectedConditions.not(ExpectedConditions.alertIsPresent()))
.wait();
and
js.executeScript("alert('something')");
boolean alertVisible = true;
while (alertVisible) {
try {
driver.switchTo().alert();
} catch (NoAlertPresentException ignored) {
alertVisible = false;
}
}
Both make FF fail horribly with an internal JavaScript exception. Possibly a bug that might get fixed (please test it, check whether it had been reported and report it if you're interested in it), so I'll leave the solutions here for future generations.
But as I said before, it's possible that it won't get fixed, since Selenium doesn't count on manual user interactions.
Not sure how this behaves in other browsers.

Selenium: Can't SendKeys() to an item that was below the visible window but was made visible by Click()

I have this problem with a text field that is visible at the time of the SendKeys. I'm using IEDriverServer.exe and C#.
Here's how I can reproduce the problem:
The text field in question is visible in the window but you have to scroll down to see it. To scroll down I click on the element using code like this:
var element = driver.FindElement(By.Xpath("…"));
element.Click();
This scrolls the window down and makes the text field visible.
But when I try to send text to now-visible window:
element.SendKeys("blah");
I get the exception:
When_applicant_enters_application.Should_be_instantly_approved_on_external threw exception: OpenQA.Selenium.ElementNotVisibleException: Element is not displayed
How can I fix or workaround this problem?
Selenium version: 2.32.1
OS: Windows 7
Browser: IE
Browser version: 9.0.15
I've written code demonstrating the problem and submitted it to the Selenium tech support volunteers.
The full discussion is at http://code.google.com/p/selenium/issues/detail?id=5620
but the take-home is:
// Doesn't work
// driver = new InternetExplorerDriver();
// driver.Navigate().GoToUrl(#"D:\CGY\selenium\Bug5620\Bug5620\Bug5620.htm");
// Works
// driver = new FirefoxDriver();
// driver.Navigate().GoToUrl(#"D:\CGY\selenium\Bug5620\Bug5620\Bug5620.htm");
// Works
driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(#"http://localhost:8080/Bug5620/"); // Hosted on Tomcat
so there may be a problem that possibly involves IE, IE security settings, Visual Studio local servers and/or the IE Driver. This may not even be a code problem, but something that needs to be documented, since other people are apparently running into the problem.
I don't know where the problem is exactly but I do have a work-around at this point, which is to use the Firefox Driver.
Thanks for your help, Jim. If you find out a better way of dealing with the problem, please add an answer here for the other folks.