How to interact with multiple browsers in the same CodedUI test - testing

I'm trying to automate a scenario where I login to a site with two users on two different browsers where further interaction will then occur in the same test. It runs up to the point where Chrome has launched the URL and the recorded steps fail giving an error that web page cannot be accessed. The recorded steps do work on Chrome if they were separated out into its testmethod of its own.
BrowserWindow window;
window = BrowserWindow.Launch(new Uri("http://example.com"));
this.UIMap.ClickOnSignInLink();
this.UIMap.TypeInValidUserCredential();
this.UIMap.ClickOnSignInButton();
this.UIMap.AssertUserIsLoggedIn();
BrowserWindow window2;
BrowserWindow.CurrentBrowser = "Chrome";
window2 = BrowserWindow.Launch(new Uri("http://example.com"));
this.UIMap.ClickOnSignInLink();
this.UIMap.TypeInValidUserCredential();
this.UIMap.ClickOnSignInButton();
Test method FeaturesTest.LiveBidding threw exception:
Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotAvailableException: The web page could not be accessed. If the page is refreshing, please wait until the page refreshes and then perform actions on it.

Only way i managed to do this is by closing the first browser. To do this you can do either of these below. Add this after IE job is finished and you want to switch to Chrome.
window.Close();
or initialize a new playback
if (Playback.IsInitialized)
{
Playback.Cleanup();
Playback.Initialize();
}

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.

How to run all scenario of same feature file in one browser? using selenium,java,cucumber

I am using hooks #Before (to initialize browser instance) and #After (to close browser instance) which means if there are three scenarios in feature file then the browser will open three times and close three times.
I want to run all three scenarios in the single browser instance.
Cucumber provides background which lets you set things ups before tests are executed.
The official docs are here. Scroll down to background (or CTRL+F background x3).
Feature File:
Background:
Given browser is initialized.
Scenerio: Test 1
Then browser is running.
And you can write the step definition as usual.

Switching to browser from a native android app with Appium

I'm trying to handle the opening browser from a native app.
I have a native android app. When I click a link on it, it opens a browser. I'm trying to switch to that browser but couldn't do it. My code block is:
Set<String> contextNames = ((AppiumDriver) webDriver).getContextHandles();
for (String contextName : contextNames) {
if (contextName.contains("android.browser")) {
((AppiumDriver) webDriver).context(contextName);
}
}
After I debugged some (for further questions)
My app contains a vebview on all pages. Here's what I get when I check with getContextHandles() on any page:
0 = "NATIVE_APP"
1 = "WEBVIEW_com.dmall.app"
If I switch context to webview and getPageSource(), I see there is an empty view.
html xmlns="http://www.w3.org/1999/xhtml">head>/head>body>iframe name="chromedriver dummy frame" src="about:blank">/iframe>/body>
(I removed some chars because of this page accept html)
I started the test again and after click the link that opens a browser and check getContextHandles(), I see
0 = "NATIVE_APP"
1 = "WEBVIEW_com.dmall.app"
2 = "WEBVIEW_com.android.browser"
Everything looks as it should be, right?
Then I'm doing:
((AppiumDriver) webDriver).context(WEBVIEW_com.android.browser);
when I check the page source, I see the same empty webview
html xmlns="http://www.w3.org/1999/xhtml">
By the way, I can't switch to other webview after this point, it says:
"An unknown server-side error occurred while processing the command. Original error: We already have a chromedriver instance running (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 134 milliseconds"
I guess this is normal
So, how can I switch to browser, any idea, solution, questions, brain storming, anything can be useful.
My capabilities is here:
DesiredCapabilities capability = DesiredCapabilities.android();
capability.setCapability(MobileCapabilityType.APP, "http://172.20.0.196:8088/android/mfandroid-debug.apk");
capability.setCapability(MobileCapabilityType.DEVICE_NAME, "AndroidEmulator" + RandomStringUtils.randomNumeric(3));
capability.setCapability(MobileCapabilityType.PLATFORM, "Android");
capability.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");
capability.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 3000);
capability.setCapability("autoAcceptAlerts", "true");
capability.setCapability("sendKeyStrategy", "setValue");
Thanks
I have faced this issue before so I think I am the right person to answer this question.
You can switch to webView of the app under test [AUT] only and not to the webView of any other app. Which means your app under test is app1 and you want to switch to webView of app2.
To switch to webView of app2 you need to change the app Capabilites at run time after you perform some actions on app1. I suggest you to do:
Close the driver once you click on the link in your first native app (I hope after clicking the link will be opened in the app2).
Re-initialise the driver with new app capabilities of app2.
Now, you should able to switch to webView of app2 (browser).

Element not being added when running test through webdriver

I am working on writing a story for a bdd framework which uses jbehave/selenium/webdriver and am having a problem where the test encounters an error while running the story but appears to be fine when running manually. I'm having a problem where javascript for the functionality I'm testing behaves slightly different when I'm running tests manually on firefox vs through selenium web driver on the same system/version of firefox and this difference is causing a js error.
I've debugged and basically the root of the problem appears to be that var request_XML_container = $('div_appendpoint_id'); returns something different when I'm running the test manually vs when I run through the bdd framework.
var request_XML_container = $('div_appendpoint_id');
request_XML_container.innerHTML = encoded_xml_from_request;
var pos = method_to_get_position('id_of_place_div_should_be_appended_to');
// JS exception is thrown saying that style is not defined **ONLY**
// when running through web driver. Running test manually on
// same system and same browser works fine.
request_XML_container.style.left = (pos[0] - 300) + 'px';
request_XML_container.style.top = (pos[1] + 25) + 'px';
request_XML_container.style.display = "block";
Why this would work fine when running manually that var request_XML_container = $('div_appendpoint_id'); would return an item with style defined, but when running through webdriver that the style attribute of the element would not be defined?
UPDATE: I had originally thought that this was updating an iframe, but I read the markup wrong and the iframe I saw is a sibling - not a parent - of the element where the response is being appended to. I'm trying to simply append the response to a div. To be honest, this only makes things more confusing as grabbing a div by id should be pretty straight forward and I'm now sure why webdriver would be producing a different return element in this situation.
UPDATE 2: Steps to reproduce and information about the system I'm on:
Use webdriver to navigate to this url: http://fiddle.jshell.net/C3VB5/11/show/
Have webdriver click the button. It should not work
Run your test again, but pause put a breakpoint at your code to click the driver
Click the button on the browser that webdriver opened. It should not work
Refresh the browser page on the browser that webdriver opened. Now, it should work.
System details:
OS : OS X 10.8.5 (12F37)
IDE : Eclipse Kepler: Build id: 20130614-0229
Browser (used manually and by webdriver) : Firefox 23.0.1
Selenium version: 2.35.0
UPDATE 3: I have provided this maven project on github to aid in reproducing: https://github.com/dkwestbr/WebdriverBug/tree/master/Webdriver
Synopsis/tl:dr; Basically, in certain situations it appears as though webdriver is overwriting the '$()' javascript method with a method that does not return an HTMLElement with innerHTML or style defined (among other things). This post details the issue and how to reproduce.
I have opened this ticket to track the issue: https://code.google.com/p/selenium/issues/detail?id=6287&thanks=6287&ts=1379519170
I have confirmed that this is a bug with the Thucydides framework (understandable since they still aren't at a 1.0 release).
Issue can be tracked here: https://java.net/jira/browse/THUCYDIDES-203

How should I test Gmaps4rails generated maps?

I am using gmaps4rails-1.3.0 to render a few maps in my rails-3.0.7 application. When the map loads, it already has a few markers set. The users get to select which type of markers they want to see by getting to click on a few checkboxes. This works fine in all versions of FF, Chrome and IE that I have checked.
So, now I wanted to write automated test cases to check this functionality. For this, I wrote rspec-2.6.0 request examples which are run through capybara-1.1.2 using the selenium-2.14.0 driver. The test is able to load the map fine with all of the markers showing up correctly. However, no markers are removed when the test unchecks one of the checkboxes. I inserted a debugger statement at the end of test and then checked the error console in the browser. It had the following message:
console is not defined
http://127.0.0.1:51643/javascripts/gmaps4rails/gmaps4rails.googlemaps.js?1323052026 Line 196
Checking the relevant file showed the following function:
Gmaps4RailsGoogle.prototype.clearMarker = function(marker) {
console.log(marker);
return marker.serviceObject.setMap(null);
};
I then noticed that during normal operation, a no. of such log messages are displayed in the firebug console. The selenium webdriver profile doesn't have firebug and nor do I expect all my users to have firebug.
Should a console.log() call be there in any javascript file post-development? If yes, what's the way to test it with a rendering driver, in particular with selenium?