Is it possible to XCTest text fields in a webview? - uiwebview

I have a webview in my app, but I need to do some UI Test, however I am unable to find the text field element to properly implement my test. Does anyone have any idea? Thank you.

You have to expose your HTML elements to the Accessibility. For the text field, you can do it this way.
<div id="text-input">Super</div>
<input type="text" aria-labelledby="text-input" value="empty" />
You should see something like this in the accessibility inspector
This is how your test may look like.
func testExample()
{
let
textField = app.webViews.textFields["Super"]
textField.tap()
let before = textField.value as? String
XCTAssertEqual(before, "empty")
// This is strange but without this sleep typeText method doesn't work
sleep(1)
textField.typeText("lorem ipsum")
let after = textField.value as? String
XCTAssertEqual(after, "emptylorem ipsum")
}
Generally, exposing HTML elements to the Accessibility is a difficult area. You can find a lot of examples here. You can also check the list of supported elements. Best of luck.

If you have placeholder or text value in the input's of WKWebview instance, you can simply do
let app = XCUIApplication()
...
let webViewsQuery = app.webViews
let textfield1 = webViewsQuery.textFields["account email"]
viewExists = textfield1(timeout: 1.0)
if viewExists {
textfield1.tap()
sleep(1) // wait for keyboard
textfield1.typeText("populated email value")
let textfield2 = webViewsQuery.secureTextFields["Password"] // for password input type
textfield2.tap()
sleep(1) // wait for keyboard
textfield2.typeText("populated password")
}

Related

autoComplete - action & onClick

I have below code, There can be a case when user types something there is no auto suggestion available, but still i want the text entered by the user.
My onClick function does gets called, but i want to get the value the user has types.
I have tried below approaches, but none seems to be working.
AutoComplete tag
<p:autoComplete id="cityInput"
value="#{myBean.text}"
style="margin-right:15px;"
completeMethod="#{myBean.fetchData}"
global="false"
forceSelection="true"
maxResults="5">
</p:autoComplete>
Button
<p:commandButton id="searchCity"
value="Search"
icon="ui-icon-search"
onclick="captureSearchText();"
action="#{myBean.search}"/>
Script
<script type="text/javascript">
function captureSearchText(){
alert("88");
//var searchVal = myAutoComplete.input.val();
//var searchVal = document.getElementById("cityInput");
//alert(searchVal.value);
//return true;
}
</script>
Edit Section
When I press ENTER key, I get the searched string in my fetchData, maybe because it is p:commandButton. But on click of button also I want to get the searched text. I hope it is clear now. What can I do to get search text on mouse click of search button. tried invoking JavaScript function.
In the completeMethod #{myBean.fetchData} just return the search string as entered by the user if the real 'searching' does not yield any results.
public List<String> fetchData(String query) {
List<String> results = searchService.search(query);
if (results.isEmpty() {
results.add(query);
}
return results;
}
There is in fact nothing 'jsf' to this, plain java creativity.
But the user still has to select it then unless the autocomplete has an autoselect option. Not sure about this. But you can also store the query string in a variable server side and use that if the user does not select anything (setting the field to required is better then imo)
public List<String> fetchData(String query) {
List<String> results = searchService.search(query);
this.queryString = "";
if (results.isEmpty() {
results.add(query);
this.queryString = query
}
return results;
}
Or move it outside the ifEmpty if you always want to store it
forceSelection="true" was an issue actually, it was forcing a selection.
I have removed it and it ran smoothly.
Answer by #Kukeltje is also good, but that is something a workaround, and forceSelection="true" was actually the root cause.

Utilizing Bootstrap's typeahead as a search function

I've got typeahead working just fine, but I am too inexperienced with the Javascript to understand how to turn the typed results into a link.
<input type="text"
class="span3"
data-provide="typeahead"
placeholder="City Search:"
data-items="6"
autocomplete="off"
data-source=["Neuchatel","Moutier"]">
So, I really just want to know how to turn the strings from data-source into links to other pages. Hopefully this is fairly simple.
thanks!
You can turn the strings into links easily..
<input type="text" data-provide="typeahead" data-source="["/foo.html","http://www.google.com","/about.html"]">
Are you also looking to take the link from the input and then navigate to the selected page?
EDIT: Navigate to item selected in typeahead..
In this case you'd define an object map that contain keys (label) and values (url) like..
var data = {
"login":"/login",
"home":"/",
"user":"/user",
"tags":"/tags",
"google":"http://google.com"
};
Then you'd initiate the typeahead. The source and updater functions would be defined to handle 1) creating the typeahead data source array from the map object, and 2) navigate to the appropriate url when an item is selected..
$('.typeahead').typeahead({
minLength:2,
updater: function (item) {
/* navigate to the selected item */
window.location.href = data[item];
},
source: function (typeahead, query) {
var links=[];
for (var name in data){
links.push(name);
}
return links;
}
});
Demo on Bootply

Avoiding Website content select and copy

I am using Drupal 6. In drupal how to avoid the user to copying the web page contents.How to disable it.
Thanks
Ultimately ... you can't.
Even if you try some fancy JavaScript or some fancy image over, etc., a user can just press Ctrl+A (select all) and then Ctrl+C (copy). There is a plethora of ways to get information from a web-site such as development environment (FireBug), alternative agents (wget/curl), or even using a browser not "protected" with the scheme.
Bottom line ... the only way to prevent someone from "keeping" that data is by not giving them access to begin with. Alternatively, make the user(s) sign an NDA/agreement and hire lawyers :-)
Happy doing productive things.
If all that is desired is prevent a "select" with a mouse, then an img-over may work. Alternatively, send back non-text (e.g. images containing the text) content and/or embed the content into Flash or another relatively controlled plug-in.
There is a java script code to disable the content copy.
I pasted that code into body of the page and set input format into php code.
<script type="text/javascript">
var donotconsidortag = ["input", "textarea", "select"]
donotconsidortag = donotconsidortag.join("|")
function unableToSelect(e) {
if (donotconsidortag.indexOf(e.target.tagName.toLowerCase()) == -1)
return false
}
function ableToSelect() {
return true
}
if (typeof document.onselectstart != "undefined")
document.onselectstart = new Function("return false")
else {
document.onmousedown = unableToSelect
document.onmouseup = ableToSelect
}
</script>
For the particular content type use " content template " module and past the above code in to content template's textarea.In this we can disable the content select option for whole content type(For ex:Page or Story)
<SCRIPT language=JavaScript>
var message = "function disabled";
function rtclickcheck(keyp){ if (navigator.appName == "Netscape" && keyp.which == 3){ alert(message); return false; }
if (navigator.appVersion.indexOf("MSIE") != -1 && event.button == 2) { alert(message); return false; } }
document.onmousedown = rtclickcheck;
</SCRIPT>
There is no way to prevent a determined user from accessing the content of your web page. Tools like firebug, and a plethora of screen capture software easily circumvent any such attempts.
To make it difficult for unsophisticated or lazy users, you could overlay a transparent 1x1 image over the top of the entire page, or content you are trying to protect.
<img src="transparent.png" style = "width:100%; height:100%;position:absolute;" />

TinyMCE Setting focus in text part

Consider the following HTML:
<div id="block-container">
<div id="some-background"></div>
<div id="text-div">Focus should be here when this HTML goes into the editor</div>
</div>
I want the caret be in the text-div -- more precisely in the first text element -- when it opens in the TinyMCE editor.
There could be a way to add some class like ".default-focused" to such element and set focus based on the class. Is there any other (generalized) way to achieve this?
The reason why I can't go with the ".default-focused" way:
1. It could be huge task to add class considering the amount of data I have and
2. More importantly, user can change the HTML and can remove the class.
Well, if you know in which element the caret is to be placed you may use this short function
// sets the cursor to the specified element, ed ist the editor instance
// start defines if the cursor is to be set at the start or at the end
setCursor: function (ed, element, start) {
var doc = ed.getDoc();
if (typeof doc.createRange != "undefined") {
var range = doc.createRange();
range.selectNodeContents(element);
range.collapse(start);
var win = doc.defaultView || doc.parentWindow;
var sel = win.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof doc.body.createTextRange != "undefined") {
var textRange = doc.body.createTextRange();
textRange.moveToElementText(element);
textRange.collapse(start);
textRange.select();
}
},

yahoo autocomplete

I'm kind of like stuck trying to implement YUI autocomplete textbox. here's the code:
<div id="myAutoComplete">
<input id="myInput" type="text" />
<div id="myContainer"></div>
</div>
<script type="text/javascript">
YAHOO.example.BasicRemote = function() {
oDS = new YAHOO.util.XHRDataSource("../User/Home2.aspx");
// Set the responseType
oDS.responseType = YAHOO.util.XHRDataSource.TYPE_TEXT;
// Define the schema of the delimited results
oDS.responseSchema = {
recordDelim: "\n",
fieldDelim: "\t"
};
// Enable caching
oDS.maxCacheEntries = 5;
// Instantiate the AutoComplete
var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS);
oDS.generateRequest = function(sQuery) {
return "../User/Home2.aspx?method=" + "SA&Id="+document.getElementById("lbAttributes")[document.getElementById("lbAttributes").selectedIndex].value +"&query="+sQuery;
};
oAC.queryQuestionMark =false;
oAC.allowBrowserAutoComplete=false;
return {
oDS: oDS,
oAC: oAC
};
}
</script>
I've added all the yahoo javascript references and the style sheets but it never seems to make the ajax call when I change the text in the myInput box and neither does it show anything... I guess I'm missing something imp...
#Kriss -- Could you post a link to the page where you're having trouble? It's hard to debug XHR autocomplete without seeing what's coming back from the server and seeing the whole context of the page.
#Adam -- jQuery is excellent, yes, but YUI's widgets are all uniformly well-documented and uniformly licensed. That's a compelling source of differentiation today.
To be honest, and I know this isn't the most helpful answer... you should look into using jQuery these days as it has totally blown YUI out of the water in terms of ease-of-use, syntax and community following.
Then you could toddle onto http://plugins.jquery.com and find a whole bunch of cool autocomplete plugins with example code etc.
Hope this helps.

Resources