Javascript Injection with Xamarin forms WebView - android

I want to change the value of an element inside a Xamarin forms WebView
I created a new project and added a WebView page that loads https://www.google.com/
I have used .Eval
m_GoogleWebView.Eval("alert('coolness!');");
This alerts fine for me. However if I try to access the dom at all, it just replaces the WebView with the text 'test text'
m_GoogleWebView.Eval("document.getElementById('lst-ib').value='test text';");
How should I manipulate elements from Xamarin Forms?

This is an issue with any WebView on Android after 4.1.
What you need to do, is assign your script to a variable like this
m_GoogleWebView.Eval("var x = document.getElementById('lst-ib').value='test text';");
This still runs the script but now just assigns the result to the variable instead of the DOM.

Related

Kony - rowTemplate returns a string on Android instead object with references to widgets in template

When I try to access the rowTemplate of a Segment2 widget in Kony 7.3 it returns a string on Android instead of an object, making it impossible to assign a gesture recognizer to the row template. Here's my assignment:
this.view.segDayViewCards.rowTemplate.flexDayViewCard.addGestureRecognizer(constants.GESTURE_TYPE_SWIPE, setupSwipe, this.onCardSwipe);
This happens inside of the controller.js file that is generated when you create a form. onCardSwipe is a function that is also in my controller.
This works just as it should on iOS, but causes the app to crash on Android because flexDayViewCard is undefined since rowTemplate is a string.
Here's the comparison of what I see on Android vs iOS
Android
iOS
Any thoughts?

Match a View in WebView from Espresso in Android

I want to match the user name text field inside a WebView which loads the Salesforce login page (but can also be applied to any other page with text fields).
I have tried with:
onView(withHint("User Name")).perform(typeText("test#sf.com"));
But that doesn't work. Any better idea?
This can be accomplished using Espresso Web 2.2 API
onWebView().withElement(findElement(Locator.ID,"username")).perform(webKeys("test#sf.com"));
Please use Locator.Xpath if you don't know ID.
To find xpath first you need to check the html code based on which you can write xpath.
To get html source code you can use chrome inspector.
connect the device to the PC and then open chrome inspector.You can right click on the html code and click on copy xpath to get the xpath.
You can fill form just run js code in WebView.
For example:
webView.loadUrl("
javascript:document.getElementById('username-field').value = 'test#sf.com’;
");

Can I design the UI in Xamarin.Forms by using XAML

I'm trying to create a cross platform app using Xamarin.Forms. As far as I know, the UI will be created from the code and the .axml file will be generated automatically.
Can I modify the .axml file to edit the UI? I tried editing but all that comes up is what is written in the code. ie: hello forms
UPDATE
public static Page GetMainPage ()
{
return new simplerow ();
}
In Xamarin.Forms you can create your pages from markup definitions that are shared across all platforms.
Typically you will write all your content pages using Xamarin.Forms, however you can mix-and-match native pages into an application should you so wish.
These shared common pages, written in Xamarin.Forms, will reside maybe in a PCL project, or a Shared Project so these can then be re-used in the platform-specific projects, each targeting a specific platform OS.
You can write these common pages, either in code, or in XAML. You can even choose to write some pages one way, and some the other if you so choose.
A Xamarin.Forms page is processed at runtime through the interpretation of the page composition that has been created.
Each control that is specified on a page, has its own platform specific renderer, behind the scenes, that will produce output that is targetted to that OS.
When writing Xamarin.Forms pages, for-the-most, you will start to learn a new way of creating pages that is abstracted from the intricacies of creating mobile applications on each different platform OS.
There is therefore no editable .axml that is generated etc as you will write your pages using Xamarin.Forms markup and controls, and even your own or other custom-controls to produce your own application pages.
The following link shows some examples of how to write XAML pages.
The following link shows some examples of how to write from code-behind pages.
Along with the previous answer re: .xaml instead of .axml, you need to remember to change the startup code in app.cs to use your new .xaml form. Replace the "new ContentPage {...};" with "new MyForm();" (where "MyForm" is the name of your shiny new XAML form).
EDIT: Downloaded the project from the dropbox link. Comments below...
I see several issues here. I think you may need to go through the walkthroughs and sample applications provided by Xamarin to get up to speed with the concepts behind XF apps.
First, you are trying to use an Activity as your application's page. In a Xamarin Forms app, it must be a View of some sort, not a platform-specific visual such as Activity.
Second, remove the "test.xml" file from your Android project's Resources/layout folder; while XAML files are indeed XML, they have an 1) have a file extension of .xaml and 2) belong in the shared project.
Here's what you need to do to get your project working: (I'm assuming you're using VS here, under Xamarin Studio, it's slightly different.)
Right-click your "testforms" shared project
Click Add from the context menu and select "New Item"
In the dialog that appears, select "Forms XAML Page" and in the Name area enter a name (such as "MyForm")
(If you're using XS, choose "New File" and "Forms ContentPage")
This will add two files to your project: a XAML file containing your layout (e.g.: MyForm.xaml), and a code-behind file (e.g.: MyForm.xaml.cs).
Open the XAML file, and modify the Label element so that the Text attribute is
Text = "Hello, World!"
Modify the body of GetMainPage in your App.cs to the following:
return new MyForm();
Run the app
Hope this helps!
You got it wrong. Forms are created either through code or XAML. No axml or anything persistent is generated at platform level, everything is done in runtime(XAML is sort of compiled at compile time).
So, modify either code or XAML if you wish to change something. Or, if you need something more demanding, than consider either subclassing an existing Renderer or create you own.

Testing html5 inputs in Android application by Robotium

At start i want to say sorry about my English skill. I write automated test for the hybrid Android application. GUI of this app is written in HTML5/JS/CSS3 (with angularJS framework). I want to test any data form. Form is filled with some data on the start (received from server). To enter new data I have to clear current value and enter the new one. For enter new text i use:
By element = By.cssSelector("...");
solo.waitForWebElement(element);
solo.typeTextInWebElement(element, "foo", 0);
and it works well. I have got problems with data clear. In the Robotium documentation i found method:
solo.clearTextInWebElement(item);
This method cannot get index parameter (I don't know why), it only use the "By" object to catch specified element. I decided to use code like:
By item = By.cssSelector("input:nth-child(3)");
this.solo.waitForWebElement(item);
this.solo.sleep(3000);
this.solo.clearTextInWebElement(item);
but still doesn't work too. I don't know how to test web inputs by Robotium.
If the web elements have name or id attributes then you can use By.id("elementId") or By.name("elementName") to access them.
Your question will be easier to answer if you post more sample code, particularly the html of the elements you're trying to test.

Android HTML Templating

Is there a way for Android to preprocess an html page (like PHP) using templates or whatever and then output the right HTML?
Example of what I mean.
Let's say I have a variable in android that holds a URL of an address or a file for JavaScript. I want to output the right script tags from Android preprocessing the file.
So in the Android activity there could be
String loc = "http://example.com/file.js"
and when I pass the loc using a webview JavascriptInterface thingy I want src from above to equal loc.
<script type="text/javascript" src="loc variable here"></script> //but how?
Also, dynamically add or remove scripts as needed. So, this sounds like a job for templates (like how Ruby On Rails ERB files work). But can Android do this? If so, how do you do it?
Thanks.
I ended up just using document.createElement("script") to dynamically load scripts instead of doing a template. It was a lot easier that way.

Categories

Resources