Android Robotium solo - how to access WebElements contained in an IFRAME? - android

Scenario:
I'm using Android Robotium Solo (v5.6.3) to automate web page interactions within my app. I need to automate data entry into INPUT fields that are contained within an IFRAME but I do not have much luck!
The Problem:
When I attempt to use, for example, solo.waitForWebElement(By.id("room-number", 5000, true) and solo.typeTextInWebElement(By.id("room-number", "101"), solo is unable to locate the element.
The discussion on this related issue "Accessing an iFrame inside a WebView #794" (https://github.com/RobotiumTech/robotium/issues/794), suggests that it's possible to use "solo.getConfig().webFrame = XXX" to focus solo on the content of a specific IFRAME and then access the WebElements. Unfortunately, I've not been able to get it to work and haven't been able to find any full examples. I assume XXX might need to be the "id" of the IFRAME but in my scenario (where I don't have control of the source code for the web pages being automated) the IFRAME tag has no id assigned.
I've created a simple example test scenario:
index.html - the main page that hosts the IFRAME
<html>
<body bgcolor="#AA3333">
<div id="wrapper">
<iframe src="embed.html" width="100%" height="100%" frameborder="0"></iframe>
</div>
</body>
</html>
embed.html - the source for the IFRAME that contains the INPUT element.
<html>
<body bgcolor="#3333AA">
<div id="page-container" style="height:100vh; width:100%;">
<label>Room Number</label>
<input type="text" name="ROOM_NUMBER" id="room-number">
</div>
</body>
</html>

After reviewing the source code for Robotium in more detail I confirmed that using
solo.getConfig().webFrame = ['id' of IFRAME as a String]
allows subsequent calls to solo.typeTextInWebElement etc. to work OK as expected.
The trick in my scenario is that the parent page assigned no id to the IFRAME so I programatically assign one at runtime using the following javascript
document.getElementsByTagName("iframe")[0].id = "test";
and then use
solo.getConfig().webFrame = "test"

Related

WebView extract and edit html

I am creating a very simple WebView application on android. However, I want to edit the html file before displaying it in the WebView.
For example, if the original html source looked like :
<html>
<body>
<h1> abc </h1>
<h2> abc </h2>
......
<h6> abc </h6>
</body>
</html>
And I want to change it to:
<html>
<body>
<h1> cba </h1>
<h2> cba </h2>
......
<h6> cba </h6>
</body>
</html>
(all "abc" become "cba")
And then, I want to display that new code in my WebView. How can I do this? thanks
I am not sure why do you need this and what kind of app it is to need this. But if you have to do it check foll code:
$(function() {
for(var i =0;i<101;i++) {
if(jQuery('h'+i).length)
jQuery('h'+i).html(jQuery('h'+i).html().split("").reverse().join(""));
}
});
First, a note on your header tags: <h100> is a common misconception for newcomers. <h_> tags are simply an organizational item for your page, and only go out to <h6> You can have multiple <h1> tags on the same page, which are just headings for that section of content (with <h2> implying a subsection of <h1>, etc).
From there, when you say "original source", I assume you mean this is your own code, correct? Not a WebView sourced from another site? If this is the case, and you are only looking to change a specific instance of a specific string in your own code, a Find and Replace should be sufficient via any text or code editor you are using.
But if this is the case, you might want to look into first learning HTML and being able to render it in a basic web browser before moving on to also trying to learn Android.

HTML page didn't get fully loaded even after using JavascriptInterface

I attempted to parse html and some portion of that dynamically generated by javascript. Using javascriptInterface with WebView worked fine on pages that used normal javascript. However, when I attempted to do same approach on pages that were controlled by angularJS, it didn't work -- data from angularJS didn't get populated. For instance,
<span class="s1">
<span class="s2">
<em class="ng-binding">{{ someVariable }}</em>
</span>
</span>
Inside curly bracket, the variable should be resolved by angularJS and its value was visible when I inspect web app through web browser. But html I saw on while processing html in javascriptInterface was following:
<span class="s1">
<span class="s2">
<em class="ng-binding"></em>
</span>
</span>
What my guess is the page was not fully loaded even with JavascriptInterface. Is there any other way to get fully loaded page from webview?

JQM - data-dom-cache="true" on page not loading js files

I got one strange behavior from JQM page.
I am using phonegap-android 4.4.2 with eclipse IDE.
Page code :
<html>
<head></head>
<body>
<div data-role="page" id="folfirstForm" data-dom-cache="true">
<link rel="stylesheet" href="../../css/follow_up.css">
<script src="../../js/addProdLine.js"></script>
<script type="text/javascript">
$(document).off("pageshow", "#folfirstForm").on("pageshow", "#folfirstForm", function() {
alert("First form loaded!");
AddElement();
});
</script>
----Some body----
</body>
</div>
</div>
</html>
You can see on code that I have put data-dom-cache="true" as per my requirement. I have also include the js file named as addProdLine.js which contains functions that i required, (for e.g. The AddElement() is from include file it self).
Now, when i am visiting this page first time all the functionality works very well. On second visit on same page I am getting the previous state of form which is expected as i have put data-dom-cache="true". But Might be due to data-dom-cache="true" my external js files are not being loaded on the second time visit on same page. I want, that on every visit ,my js file should run.
Jquery mobile only loads tag once when whole document is loaded. After the document is loaded if you will click on other links then only content part data-role=page will be refreshed through ajax along with data transition effect. Hence either add all your js script in a file and add it into head tag or stop loading other pages through ajax using following option:
<a href="#" data-ajax=”false”>Without Ajax</a>
But if you will disable content loading using ajax then link will work as it works in normal webpage and also transition effect will be not exist.
Edit:
One Alternate way could be: you can append some random values to the path of your JS files.
<script src="../../js/addProdLine.js?Q=<?php echo time(); ?>"></script>

Unable to switch to IFrame using Android WebDriver

I was wondering if someone could point me to what I am doing wrong exactly. I am trying to access an iframe using Android WebDriver. The iframe opens up when I click on a Sign in link on the home page. Below is a snippet of the src code that has the iframe id that I am trying to access-
<div class="dialog" style="background-color: rgb(232, 235, 238); color: rgb(51, 51, 51);">
<iframe id="registration-dialog-frame" scrolling="no" frameborder="0" src="http://test.com" border="0" allowtransparency="true" style="width: 235px;">
<html>
</iframe>
<span id="signin-cancel" class="cancelButton">Cancel</span>
I wait till the iframe opens up, check if the frame id is present and then use switchTo() to switch to that iframe like this
driver.switchTo().frame("registration-dialog-frame");
I get a
org.openqa.selenium.WebDriverException: Error: {"message":"Cannot read property 'document' of undefined"}
When I try this
driver.switchTo().frame(driver.findElement(By.id("registration-dialog-frame")));
I get a
org.openqa.selenium.WebDriverException: java.util.ArrayList cannot be cast to org.openqa.selenium.android.library.DomWindow
Is this a Android WebDriver thing? Has anyone else come across this similar kind of errors when switching to iframe? Does anyone know how I can do this?
Check the number of frames present in that page by looking at code and use index to switch into that particular frame as shown below.
driver.switchTo().frame(frameindex);
Ex: To Switch to the first frame, use below line.
driver.switchTo().frame(0);

iframe is not working android inbuild browser how can resolve issue?

I am working on a cross platform mobile application using phonegap (html,javascript) problem is selected item related image not display in iframe and this image display another page please see below code
<html>
<head>
<script>
function onchangeevent(mySelect)
{
PageIndex2=mySelect.selectedIndex;
{
if
(
mySelect.options[PageIndex2].value != "none"
)
{
frames['iframe2'].location.href = mySelect.options[PageIndex2].value;
}
}
}
</script>
</head>
<body>
<form name="form">
<p><select NAME="selectimage" SIZE="1" onChange="onchangeevent(this.form.selectimage)">
<option VALUE="none" SELECTED>Select a page and go</option>
<option VALUE="ic_launcher.png">one</option>
<option VALUE="icon.png">two</option>
</select> </p>
<p>
<iframe src="" name="iframe2" height="100%" width="100%">You need a Frames Capable browser to view this content.</iframe>
</p>
</form>
</body>
</html>
iframe tag not working android inbuild browser how can resolve the issue ? please tell any alternative tags supported all browsers.
Android browser supports iframe. I suspect it's one of the other attributes that may be causing an issue. I know scroll attribute can cause the iframe to not appear. Try a simpler version of the iframe with just the href and a width and height and see if it appears.
From the looks of it, the i-frame is working, it's most likely the select script, try using radio buttons see if that works.
For some reason several types of browsers are picky with the select form.

Categories

Resources