I've set up my webview properly and I execute my Javascript in the onPageFinished method.
The problem is that whenever I try to change the value of my input, no matter what I do, I end up with the entire webview being replaced by the value of the input that I'm trying to replace.
view.loadUrl("javascript:document.forms[0].elements['password'].value = 'test';");
Any ideas?
PS. This code in the same place works perfectly.
view.loadUrl("javascript:checkForm()");
This executes the checks and submits the form. I get a popup "Please enter a value" after running this.
I have also tried document.getElementsByName("password")[0].value = 'test'; Same result.
RESOLVED: It was the emulator causing the problem. I tested directly on my phone with USB debugging switched on and it works perfectly.
Your first line of code works for me using id or name attributes:
view.loadUrl("javascript:document.forms[0].elements['password'].value = 'test';");
the second line, works with getElementById but doesn't with getElementByName:
view.loadUrl("javascript:document.getElementByName('password').value = 'test';");
So, that's not the problem. Maybe if you post the rest of the code.
You try to change the view, so you need to use:
.dispatchEvent(new Event('input')) ",null).
Try this:
view.evaluateJavascript("javascript: " +
"var password= document.getElementById('password'); " +
"password.value = 'test'; " +
"password.dispatchEvent(new Event('input')) ",null);
Related
I`m building a discord bot with jda, made a method to use mXparser to get a math operation as an input from the chat e.g: /math 5+1
wrote everything to get the message, separate the arguments from the input on the chat, everything works until I put the code inside an IF statement that checks if it actually starts with "/math", the code inside it uses mXparser to calculate everything and send it back to chat.
Tried just about everything I could think of, taking all variables off the method, rewriting everything, I don`t get any errors either as stack trace or in the code editor, it just doesnt go through, tried just printing everything and it works fine as well, printing all the values on the console, everything seems to be right.
This part is where I check for the message, get the Strings and trim everything
public void onMessageReceived(MessageReceivedEvent event) {
this.messageReceived = event;
this.prefix = Main.prefix;
checkPrefix = messageReceived.getMessage().getContentRaw().split("\\s" + prefix);
mainArg = checkPrefix[0];
checkArgs = messageReceived.getMessage().getContentRaw().split(" ");
callAllCommands();
}
Here is the command to take the actual expression from the chat input calculate it and send it back.
private void mathCommand() {
mathexp = new Expression(checkArgs[1]);
messageReceived.getChannel().sendTyping().queue();
essageReceived.getChannel().sendMessage(Double.toString(mathexp.calculate())).queue();
}
This is inside the callAllCommands() method, that is how it is supposed to work, if the command on the chat is /math then the expression e.g: /math 1+1 it will send the result back, if I take off the IF statement it works just fine but then I can't check for the command. The other commands do work fine with the IF statement
if (mainArg.contentEquals(prefix + "math")) {
mathCommand();
}
I don't really get any errors, it just does not work, sorry if I missed something really simple, i`m not that experienced yet.
I am trying to finish a test script written in Java. Everything works great and I am trying to print out a confirmation if all previous steps runs which says that account creation was successful.
To do this, I have put the element in a string and then print out the element using the System.out.println command. But for some reason , all I am getting is the following
CONFIRMATION:
That's it. It's supposed to show the string text which reads " Customer information added successfully" . I been trying for the past 2 hours to get it to print it. Can someone please help ?
Here's my code that's causing the problem.
String conf = driver.findElement(By.id("MainContent_lblTransactionResult")).getText();
System.out.println("CONFIRMATION: " + conf);
I have checked and rechecked the element id and it is the correct element id.
https://imgur.com/RUPN3Ea ( Confirmation page )
https://imgur.com/c2YtIAD ( Eclipse output )
Try this
String conf = driver.findElement(By.id("MainContent_lblTransactionResult")).innerHTML;
What you are trying to pull out of the element is in the innerHTML
Use some delay like Webdriver wait.until or Thread sleep after hitting the submit. Try to get text using JavascriptExecutor like below:
JavascriptExecutor jse = (JavascriptExecutor)driver;
String conf = jse.executeScript("$('#MainContent_lblTransactionResult').text();", "");
System.out.println("CONFIRMATION: " + conf);
You can also try using .html() instead of .text()
String conf = jse.executeScript("$('#MainContent_lblTransactionResult').html();", "");
Hopefully it resolves your issue.
I'm creating an alert which should display a variable inside its display message.
So I've created a R.string.dialog_message1 and a R.string.dialog_message2 to put before and then this variable. This is what I achieved:
builder.setMessage(R.string.dialog_message1 + lastOnes + R.string.dialog_message2)
.setTitle(R.string.dialog_title );
I get no errors until I get to the runtime process.
It displays:
"Submit query" instead that my own mixed message, any ideas?
Your code should look like:
builder.setMessage(getString(R.string.dialog_message1) + lastOnes + getString(R.string.dialog_message2)).setTitle(getString(R.string.dialog_title));
Your R.string.stringname1 calls are returning the String resource ids, not the Strings themselves. This example from Android will show you how to properly use String resources.
I'm currently testing an application using Appium on an android device (appium version: 1.2.4.1, java-client: 2.1.0). I'm using the following code to send some text in a textField:
driver.findElement(By.name("Name")).sendKeys("My Name");
and it works fine just it takes it too long to actually send the text on the textbox (usually 7 seconds). I was wondering does anybody know another way to send text on a textField that takes less?
Thanks!
I solved this issue by using adb to send text instead of appium!It is really fast!
try {
textElement.click();
new ProcessBuilder(new String[]{"adb", "-s", "YOURDEVICEUUID", "shell", "input", "text", "YOURTEXTASINPUT"})
.redirectErrorStream(true)
.start();
} catch (IOException e) {
e.printStackTrace();
}
Same way you may use this for Click,clear,install,uninstall etc.. there may be some need to sleep thread for sync issues but it is only 50ms which is too less than 5 seconds which appium takes!
You may use DDMLIB to make this adb call instead of ProcessBuilder!
Try :
driver.findElement(By.name("Name")).Click();
driver.Keyboard.SendKeys("My Name");
This should run faster then your method.
This capabilities helped me to reduce the time of inputs on Android
desiredCapabilities.setCapability("ignoreUnimportantViews", true);
desiredCapabilities.setCapability("disableAndroidWatchers", true);
You can find more here https://appium.io/docs/en/writing-running-appium/caps/#android-only
Experiencing slow automation on Appium is common because Appium is based on a client/server architecture. Network issues can influence the performance of a test (unless you are running your test in the same machine where Appium is installed).
I can tell you that I have also experienced problems with slow tests on Appium. It usually happens on simulators/emulators by the way.
Send keys as part of a UX scenario
If your test needs to send keys as part of a User Experience scenario, then SendKeys is your only option. This method does not simply set a value in a textbox, it actually behaves like a user pressing keys and sending keys to a textbox.
If this is what you need, then you need to understand what is happening a network level because this is what your problem is about. Also consider that this method can be slow on its own sometimes (this is my experience).
Setting a text is not important for the UX scenario being tested
In case the step of setting a textbox's value is not a core part of your automation for the specific test being considered, you an always do achieve this by means of ExecuteScript which lets you execute a Javascript code in your app. I am assuming you are automating the WebView context.
int result = driver.executeScript("
try {
var el = document.getElementById('<your-txtbox-id-here>');
el.value = '<your-text-here>';
return 0;
} catch {
return 1;
}
");
Java does not support multiline strings so the previous is a prettyprint of the following:
int result = driver.executeScript("try{var el = document.getElementById('<your-txtbox-id-here>');el.value = '<your-text-here>';return 0;}catch{return 1;}");
This method will return 0 in case the string was successfully set, otherwise 1. It should be faster because the driver will not send each key separately but execute the script in an anonymous function and get back its return value.
Try to add the following capabilities inorder to have appium keyboard(and not the physical keyboard)
capabilities.setCapability("resetKeyboard", true);
capabilities.setCapability("unicodeKeyboard", true);
Replace sendKeys with the setValue method available in later versions of appium:
driver.findElement(By.name("Name")).setValue("My Name");
It is much faster in my experience.
For new commer, in the Appium version 1.9~, both method executeJavaScript() & setValue() works so good, and you can consider to use it.
// use js
executeJavaScript("$('#" + fieldId + "').val(testData);
// use setValue
$(By.id(fieldId)).setValue(testData);
I improved the speed of my test (written in Python) using:
driver.set_value(myElement, "My Name")
instead of:
webElement.send_keys("My Name")
If you are using Java, it will be something similar to:
driver.setValue(driver.findElement(By.name("Name")), "My Name")
Another approach could be with adb... (This is the fastest one but you have to use another thing besides appium)
//1st - Click at your WebElement
driver.click(driver.findElement(By.name("Name")))
//2nd - Using adb send your text
//adb shell input text "My Name"
adb shell input keyboard text "My Name"
I'm updating a Selenium program I wrote a while back and part of it has stopped working. I want to go through a whole series of links on a page, click on each, making sure that some expected text is present. But sometimes a log-in page (https://library.med.nyu.edu/sso/ezproxy_form.php) appears before the desired page, in which case I need to log in before checking the page. The problem is, no matter what string I put in to check whether I've landed on the log in page, Selenium concludes it's not present and skips logging in, obviously causing everything else to fail. See below--I'm not sure that was actually the problem. It seems to be instead that it's rushing through the "if we need to sign in" code without actually signing in, then obviously failing the main part of the test because it's not on the right page.
Here's the code--does anyone see my mistake?
for (int i = 0; i < Resources.size(); i++) {
try {
selenium.open("/");
selenium.click("link=" + Resources.get(i).link);
selenium.waitForPageToLoad("100000");
if (selenium.isTextPresent("Please sign in to access NYUHSL e-resources")) {
selenium.type("sso_user", kid);
selenium.type("sso_pass", password);
selenium.click("name=SignIn");
selenium.waitForPageToLoad("100000");
}
if (!selenium.isTextPresent(Resources.get(i).text)) {
outfile.println(Resources.get(i).name + " failed");
}
} catch (Exception e) {
outfile.println(Resources.get(i).name + " could not be found--link removed?");
}
}
Does the login page have a page title? If yes, try validating the page title using selenium.getTitle() method to check if you are headed to login page. If not, proceed clicking on the link without logging in.
I think page title validation can help resolve this issue
Try putting:
selenium.setSpeed("1000");
Right after the selenium.open this will inject 1 second delay (1000ms) between selenium commands. You should make it a standard practice to add this, especially if you're not running headless browsers.
Also you might consider using, since you know the url you are expecting to be on if on the login page, the selenium command getLocation. This will return the absolute URL of the current page. Might be more effective than trying to look for elements that can change at any time within the page.
So to use getLocation in your code above:
if (selenium.getLocation() == "your reference url"){
do your login stuff here
}
Again this is just a sample to illustrate what I'm saying. Hope it helps you out.