Protractor sendKeys issue with scripted input fields - selenium

I'm automating e2e tests with Protractor on an angular app.
However, I have an issue when sending keys on input fields.
The sendKeys would miss few characters everytime so I found a workaround :
static sendKeys(value, element){
value.split('').forEach((c) => element.sendKeys(c));
}
This works well but it takes more than 3 times the time the original sendKeys function would.
Well no problem my tests are still functionnal right ?
My app now has new fields with scripts behind them.
One of them is a datepicker input, you can either choose from the datePicker or type it manually. However, for today's date you would type 09022018 and the slashes are automatically appended at the right place (like so 09/02/2018). If you were to enter a wrong date the field is cleared.
Now back to the problem : it seems that both my implementation of sendKeys and the original one loose focus after each submitted key. This means that I can't enter a valid date in the input field as it's cleared after each simulated keypress.
I could use browser.executeScript to fix it but I wouldn't be able to test the functionnality adding slashes. Also, as you type, the datepicker is still open and refreshes after each keypress, you can select a date from it at any time and that is also a feature I want to test.
Thanks in advance

Use executeScript to set the date in backgrond, then use sendKeys to enter a space or Tab at the end to trigger the Keyborad event which will check the input and format the input with slash
function enterDate(date) {
var script = 'arguments[0].value=arguments[1]';
// input box for date
var dateBox = element(by.xxx(yyy));
browser.executeScript(script, dateBox, date);
dateBox.sendKeys(" ");
// or try send Tab
dateBox.sendKeys(protractor.Key.TAB);
}
enterDate('09022018');
You can try this solution on other fields you fixed but take 3 more time.

Related

Selenium select date from a calendar- unable to find x path

I am trying to select a date from the calendar. However sendkeys does not send data. After I click on the field calendar month drops down. Right click reveals nothing. I have tried in Firefox and Chrome.
http://demo.guru99.com/V4/manager/addcustomerpage.php
username: mngr284483
password: AjerYbu
#FindBy(xpath = "//input[#name='dob']")
private WebElement selectCustomerDOB;
public void sendDOB(String dob){
selectCustomerDOB.sendKeys(dob);
}
I have tested with Thread.sleep to make sure it is not a timing issue.
What can I do in this case to select a date? Thanks in advance for your time and ideas.
Since you are using an element with type 'date', it is up to the browser to render the calendar, or anything else to allow users to select the date. The date picker is not part of your web page and so you would not be able to find its locators in the conventional ways.
I believe your testing requirement is to be able to check how your application behaves when different dates are selected, rather than to see if the date picker works (which the browser would take care for you).
In order to select dates, you should be able to use send keys if you gave the value in the right format (yyyy-MM-dd). I tested with the given website using Selenium IDE and things seem to work.
WebElement selectCustomerDOB = driver.findElementById("dob"); // While xPath is great and your xPath is valid - I feel ID is much faster.
selectCustomerDob.sendKeys("2011-12-21");

How to input the value of a hidden field into a textbox with Test Cafe Studio?

I am attempting to input the value of a hidden field into a textbox in Testcafe, ideally in some sort of manner that simulates typing. Is there a way to do that? Every time I try to do it via javascript it just throws a javascript error.
Essentially I am testing a pretty standard web app - I fill out a form, go page to page, and then must type in a value that is kept in a hidden html input field on the page. I honestly have no idea where to start - every time I've tried to do this with javascript via the "Run Test Cafe Script" it has thrown a javascript error - I really don't know where to start if javascript can't be used.
TestCafe cannot type text in a zero-size input element. I suggest you try the Run TestCafe Script action with ClientFunction that puts a value to the input element directly:
const setValue = ClientFunction(() => {
document.querySelector('input[type="hidden"]').value = 'John Smith';
});
await setValue();

instead of sendkeys last copied item from clipboard is getting entered in the element

In my application I have an email field and I'm using sendkeys to send an email address, but instead of sendkeys selenium inserts the value from clipboard. Do anyone faced this issue?
code:
WebElement email=driver.findElement(By.xpath("//div[#class='auth-container']//input[#type='text']");
email.sendKeys("sample#sample.com")
since Id is dynamic I have taken xpath from parent container
In Node.js javascript, on hungarian keyboard, the # sign in sendKeys has replaced with the content of clipboard. If you try to replace "#" width its unicode value "\u0040", the result is same, the sendKeys has has replaced with the content of clipboard, even though try to the tricky Key.chord( Key.ALT, "v", Key.NULL ) form, it does nothing.
// if clipboard contains XXX
WebElement email=driver.findElement(By.xpath("//div[#class='auth-container']//input[#type='text']");
email.sendKeys("sample#sample.com")
// result: field content is sampleXXXsample.com
If you put # sign on the clipboard, it works well, but you cant guarantee the content of clipboard in test automation. There is a way, to send keys to the active item.
I would avoid to fill the field with javascript, as mentioned above, because javascript fill it directly, does not care with the field status, fill it with value even though the field is inactive, do not triggers events, etc, javascript is brute force fill the field.
Send. Keys. There is a npm package for it. It is a wrapper for powershell sendkeys, so works only on windows.
npm install -g sendkeys
and the test code:
const sendkeys = require( 'sendkeys');
WebElement email=driver.findElement(By.xpath("//div[#class='auth-container']//input[#type='text']");
email.sendKeys("sample")
// result: cursor is in the field, field contains 'sample'
sendkeys.sync( "#sample.com" )
// result: field content is 'sample#sample.com'
If send keys is not working try using action chains. It is often a good way to solve selenium interaction problems.
from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains(driver)
email = driver.find_element_by_xpath("//div[#class='auth-container']//input[#type='text']"))
action.send_keys_to_element(email, "sample#sample.com").perform()
hope this helps
I am run script in java, hope this help you..
Using javascript I successfully send data into textbox.
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('identifierId').value='admin'");
Another way :-
WebElement Username = driver.findElement(By.xpath("//*[#id=\"identifierId\"]"));
((JavascriptExecutor)driver).executeAsyncScript("arguments[0].value='admin'",Username);
Here we are passing WebElement as a argument to the javaScript.
For more information refer this link
Another way is using robot class ..Common solution is to use the clipboard, Copy text and paste that text into text field.
String username = "admin";
StringSelection stringSelection = new StringSelection(username);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
This doesn't type the entire "string" but helps to type whatever you want other than one character at a time.
String un="admin";
char c;
int l=un.length(),v1=0,v2=0;
//System.out.println(l);
while(v1<l)
{
c=un.charAt(v1);
v2=(int) c; //converts character to Unicode.
robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(v2));
Thread.sleep(150);
robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(v2));
v1++;
}
see it works perfectly and it's awesome! Though it doesn't work for special characters which cannot be traced by unicode like |,!...etc.
I have encountered this, but it seemed to be intermittent. The code that was working well for months, suddenly doing the same thing (pasting from clipboard in place of every # aka "at sign" ).
I was able to make it go away (won't say "fix") by updating Chromedriver. I suspect that this was because Chrome has auto-updated, but the Chromedriver didn't. Can't be sure, because also had to reboot in the process.
In any case, this is not "normal". Email addresses with # sign should be passed through sendKeys without problems.
This works for me:
chromeoptions.AddUserProfilePreference("settings.language.current_input_method", "US");

TestCafe does not write in text input field

I'm using TestCafe for test automation of a web application based on the Wicket framework. I try to type text into a text input field ... well, actually it is a dropdown list, where a text input field appears, so that the user can search for certain codes. The HTML fragment is as follows:
HTML fragment
And here is the corresponding screenshot (text field above "001"):
Text input field with dropdown
The user can type some characters and the list below is automatically filtered (I did this manually):
Text input field with some text
My TestCafe test tries this:
.click( productcodeList )
.expect( productcodeInputField.visible ).ok()
.click( productcodeInputField )
.typeText( productcodeInputField, 'ABW' )
i.e.
Click on the drop down list.
Assume that the text input field is now visible (works fine).
Click on the text input field (this should not be necessary, since typeText() is supposed to do this anyway).
Type the text "ABW" into the text input field ==> This does not work.
I'm sure that my Selector works, since the assertion (expect) is successful and when I debug the test run after the second click (on the text input field), I see the following:
TestCafe screenshot
I.e. the cursor is directly on the text field, but somehow TestCafe cannot write the text into the field.
Some additional information: The Selector for the input field is created as follows:
productcodeInputField = Selector('span').withAttribute('class', /select2-dropdown.*/ ).child('span').withAttribute('class', /select2-search.*/ ).child('input').withAttribute('class', 'select2-search__field' );
More information: I'm using the same logic on the same page:
kurzbezeichnungField = Selector('input').withAttribute('name', /.*aeAbbreviation.*/);
...
await t.click( kurzbezeichnungField )
.typeText( kurzbezeichnungField, 'xxxWWW' )
and this works fine.
Node.js version: v10.16.3
Testcafe version: 1.5.0
This issue looks like a bug. However, I cannot say it precisely without an example that demonstrates the problem.
My team would really appreciate it if you share your project or sample to demonstrate the issue.
Please create a separate issue in the TestCafe github repository using the following template and provide as much additional information as possible.

How to sendkeys "time" in time type element?

I need to pass data in time format in "time" type element in "10:00 AM" format.
I am using following code:
public static void setShift()
{
txttime.sendkeys("1030AM");
}
this is not working. what is a correct way to enter such data?
Use Following Code :
It will work it for textbox/text area control
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a");
Date date = new Date();
txttime.sendkeys(date);
For the HTML input type datetime-local handling it from selenium is not ideal. It is not the most used date time picker, and it is not supported in firefox or safari.
For chrome, the date time format shows the format set in the browser's machine.
If you haven't changed anything, I'm guessing you are getting the format shown in the guru99 tutorial.
If that is the case, then you have missed that they also have provided the solution there.
After entering the date part you need to press tab to input the time part. Which is missing from your given code.
Try this:
First, input the date
WebElement dateBox = driver.findElement(By.xpath("//form//input[#name='bdaytime']"));
dateBox.sendKeys("09252013");
Second, press tab
dateBox.sendKeys(Keys.TAB);
Last, input the time
dateBox.sendKeys("0245PM");
Note:
If your machine has different DateTime formatting then this might not work. You have to check which part of the date time senKeys can actually input then split up that part and use Keys.TAB to press tab. Then input the next part until completion.