How to make text import into fields in webview by testing via espresso? - webview

I have activity, and activity create webview from AdvancedWebView.
webview load url: webview.loadUrl
webpage from this url contains two fields for input.
How can I insert test values into this fields?
I have esspresso test.
lateinit var scenario: ActivityScenario<MainActivity>
#Test
fun myTest() {
val intent = Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java)
scenario = launchActivity(intent)
onWebView()
.withElement(findElement(Locator.ID, "id")).perform(webKeys("test"))
}
I have such code, but nothing happens.

Related

getting profile data from auth0 hooks into nextjs getInitialProps or getServerSideProps

I want to load data form an Auth0 useUser hook, which can only be called from a page functional component.
that's fine but when i return jsx, the hook call is async. and the data is not available to be used when the jsx component is rendered (ie return <Home user={data} /> the data is from a hook call and Home renders before data is available) and data is only available after Home is rendered.
Please is there a better approach to achieve this?
I want data from hooks to be available during rendering of a page.
import getUser from './api/hooks/user';
import Homepage from './home';
function Controller ({userAuth}) {
//2. it is only acceptable to be called here
//let user = getUser();
return <Homepage user={userAuth} />
}
Controller.getInitialProps = async (context) =\> {
//1. this is wrong cause nextjs disallows it
let user = getUser();
//3. but the getInitialProps, getStaticProps and getServerSideProps passes data to the page component as //props and renders the page only when data is available.
return{userAuth: user}
}
export default Controller;
//4. Also I Used
import { getSession } from '#auth0/nextjs-auth0';
Controller.getInitialProps = async (context) =\> {
//5. the user object from getUser Hook is present there.
let user = getSession();
//6. but sometimes user.user returns undefined
return{userAuth: user.user}
}

Does elementHandler in jspdf fromHTML support multiple elements?

I am using jspdf to convert an HTML page to PDF using fromHTML(). The HTML page includes multiple images, which I need fromHTML() to ignore in order to generate the PDF.
I want to use the elementHandler to ignore the images. However, I can only get that to work with a single element ID. Here is the way the documentation shows:
var elementHandler = {
'#ignorePDF': function (element, renderer) {
return true;
}
};
I have tried to replace the '#ignorePDF' ID reference to a reference with a class that applies to all of the images:
'.ignorePDF'
or to include multiple ID's (one for each image):
'#ignorPDF1,#ignorePDF2'
but neither of those approaches has worked for me. Is there another way to accomplish this?
i figured out both issues. To reference multiple items to ignore, set it up like this:
var elementHandler = {};
elementHandlers["#img1"] = function...
elementHandlers["#img2"] = function...
also best to create a function that you can reuse rather than defining it over and over.
As for the inability to use a variable for the key, that was a dumb javascript error on my part. The variable name can be used like this:
var img1 = "#img1";
elementHandlers[img1] = function...
The # character must be included.
It would be useful if the method were modified to permit a class value to be entered so that a single class could be used to denote all items to be ignored.
You can ignore all objects of the same class (called for example no-print), as follows:
var noprints = document.getElementsByClassName("no-print");
var elementHandler = {};
for (var i=0; i<noprints.length; i++) {
elementHandler['#'+noprints[i].getAttribute('id')] = function (element, renderer) { return true; }
};

Pass a URL into a Dart app

I have a page with links. These links all end in the same way. For example www.site.com/fruit/apples, www.site.com/fruit/bananas, www.site.com/fruit/oranges, etc. I want all these links to call the same Dart app and have the app do some processing and then redirect you wherever you need to go (the bananas page vs. the oranges page). This way, I avoid having an actual HTML file for every single fruit. I can instead have a single landing template that gets populated with variable fruit data.
The part I'm hung up on is passing the url into the Dart app so it can do the handling. I understand main() cannot receive arguments, so what's another way?
You can use the route package to handle the URL's for you.
For example:
import 'package:route/client.dart';
final fruitUrl = new UrlPattern(r'/fruit/(\w+)');
main() {
var router = new Router()
..addHandler(fruitUrl, showFruit)
..listen();
}
void showFruit(String path) {
var fruit = fruitUrl.parse(req.path)[0];
// Display the page according to the fruit type
}
If you don't need to handle actual routes, and you just want to handle any query parameters passed of the form ?fruit=apple you don't have to use the routes package and can instead manually parse the URL:
Map params = {};
// If arguments were provided, decode them into params map
if(window.location.search.length > 1) {
// Break all arguments into form: fruit=apple
List<String> queryPairs = window.location.search.substring(1).split('&');
for(String queryPair in queryPairs) {
// Add arguments into params map: key=fruit, value=apple
List<String> queryPairList = queryPair.split('=');
params[queryPairList[0]] = queryPairList[1];
}
}
// Handle the proper action based on the fruit
switch(params['fruit']) {
case 'apple':
// ...
break;
// ...
case 'orange':
// ...
break;
}

Reading multiple inputs from Dart Console

I want to read different data from the console in a Dart server application, like:
forename : user inputs and enters
lastname : user inputs and enters
age : user inputs and enters
The following code works, but for 1 input item only:
var stream = new StringInputStream(stdin);
stream.onData = () {
voornaam = stream.readLine();
};
But I can't get it to work for multiple items.
Is there an easy way to do this in Dart ?
Thanks!
Since you're using a StringInputStream rather than just a standard InputStream, and because you're looking to read text. Unless there's a particular reason, I would recommend using the onLine handler over the onData. On data will basically try to 'stream' the information in that it's called immediately not on a new line itself. Try something like the following (note, not complete code, missing proper error handling etc.)
#import('dart:io');
main() {
var stream = new StringInputStream(stdin);
stream.onLine = () {
var str = stream.readLine().trim();
print(str.toUpperCase());
if(str == 'EXIT') exit(0);
};
}
One other note to point out, if you ever are data-streaming and using the onData handler, it is recommended that you then use the read method, as opposed to the readLine method to retrieve your content, again due to the nature of onData not waiting for a full line of text to be received before being called.
import 'dart:io';
void main() {
print('-----welcome-----');
print('what is your firstname');
var fname = stdin.readLineSync();
print('what is your lastname');
var lname = stdin.readLineSync();
print('what is your age');
int age = int.parse(stdin.readLineSync());
int left = 100 - age;
print('your name is $fname $lname and you are $age years');
}

How to display textarea's data in a table

I am using ASP.MVC 3. I have a view that has a textarea on it. I captures data in it, and when I want a new paragraph I wil press enter twice. After all my data is entered I save the text to the database.
In my details view I would display the data in a like:
<tr>
<td valign="top"><label>Body:</label></td>
<td>#Model.Body</td>
</tr>
Now the text displays as 1 paragraph even though in my textarea (when I captured the data) it seemed liked paragraphs.
How would I get the data to display as paragraphs in my table like what I captured it in my textarea. I'm assuming that I have to search for carriage returns and replace them with break tags?
I'm assuming that I have to search for carriage returns and replace them with break tags?
Yes, your assumption is correct. You could use a custom HTML helper:
public static IHtmlString FormatBody(this HtmlHelper htmlHelper, string value)
{
if (string.IsNullOrEmpty(value))
{
return MvcHtmlString.Empty;
}
var lines = value.Split('\n'); // Might need to adapt
return htmlHelper.Raw(
string.Join("<br/>", lines.Select(line => htmlHelper.Encode(line)))
);
}
and then:
#Html.FormatBody(Model.Body)
UPDATE:
And here's an example of how this method could be unit tested:
[TestMethod]
public void FormatBody_should_split_lines_with_br_and_html_encode_them()
{
// arrange
var viewContext = new ViewContext();
var helper = new HtmlHelper(viewContext, MockRepository.GenerateStub<IViewDataContainer>());
var body = "line1\nline2\nline3<>\nline4";
// act
var actual = helper.FormatBody(body);
// assert
var expected = "line1<br/>line2<br/>line3<><br/>line4";
Assert.AreEqual(expected, actual.ToHtmlString());
}

Resources