Karate UI: Entering input using variable values - karate

KarateUI question
I'm trying to enter values in a text field using a variable. Example:
* def foo = bar
* waitFor("input[aria-label='Search Input']").input('<foo>' + Key.ENTER)
This results in value being entered in the Search Input field.
I have been using the '<[something]>' successfully on a number of other places, not sure why it's not working in the above example.
I have tried a number of other approaches following the documentation without much luck.

Karate "variables" can be mixed into plain JavaScript. So try this:
* def foo = 'bar'
* waitFor("input[aria-label='Search Input']").input(foo + Key.ENTER)
That's right, no angle-brackets required.
Also see: https://github.com/karatelabs/karate#scenario-outline-enhancements
If still stuck, follow this process please: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue

Related

Is it way to wait some text via attribute

I would like to wait some text in the following format:
waitUntil(locator, attribute, attributeValue)
Examples from tutorial page do not meet the necessary conditions:
And waitUntil('#eg01WaitId', "_.innerHTML == 'APPEARED!'")
And waitForText('#eg01WaitId', 'APPEARED')
Take some time to read and understand the documentation. What you are looking for is this:
* waitUntil('.some-class', "_.getAttribute('some-attribute') == 'some-value'")

Karate testing UI : what is syntax entering random value in text field

I am trying to enter a random string in text box using the input(“X-path”,’#(name)’). Script is failing with invalid syntax .
Can anyone help me with the correct syntax
Here is the working sample script created for POC
Feature: create order
Background
def now=function(){return java.lang.System.currentTimeMills()}
*def ordername='order'+now()
Scenario: Create random order
Given call read('login.feature')
Given driver 'url to navigate to another page'
And click('#selector')
And input("x-path",ordername)
I think you missed the fact that variables can be used "as is" in JS functions.
* def name = 'foo'
* input('/some/path', name)
The '#(name)' syntax is only for JSON: https://github.com/karatelabs/karate#rules-for-embedded-expressions

Ignore case Xpath #Name attribute c# Selenium/Appium

I have a C# selenium/Appium project where I need to find a desktop Application window By.Xpath("").
This works:
By.XPath("//*[#Name='ASDASD']");
However, some builds of the app have the window name be "ASDasd", which causes the Xpath above to not find the window element and the test fails.
Is it possible to Ignore the case of the #Name attribute whether it be "ASDASD", "ASDasd" or something else?
I did try using the XPath translate function, but I am not able to find the element, I assume I am doing it wrong.
What I tried:
By.XPath("//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'asdasd']")
or
By.XPath("//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'asdasd']")
or
By.XPath("//*[#Name='translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'asdasd'']")
or
By.XPath("//*[#Name='translate(asdasd,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')']")
Maybe some other variations too, but I could not get it to work.
Some of the examples may have invalid formatting.
While other seems to be valid but could not find the element and it would timeout.
UPDATE:
Thank you for the assistance, this worked:
By.XPath("//*[translate(#Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='asdasd']");
However, it added 60 seconds to the test somehow, it seems to stall for 60 seconds on one of the places where it looks for the main window.
Thanks for the help!
Regards
name() gives you the name of context node. In this case (//*), the name of whatever element you are currently looking at. You meant to write #Name, i.e. the attribute that happens to be called Name.
By.XPath("//*[translate(#Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'asdasd']")
Using translate() is clunky and fails when the search string contains unanticipated characters.
Unfortunately, there is no lower-case() function in XPath 1.0. But you can work around this limitation with the help of the host language (such as C#).
The following will dynamically create an XPath expression which finds arbitrary values case-insensitively:
var searchValue = "asdasd";
var uc = searchValue.ToUpperInvariant();
var lc = searchValue.ToLowerInvariant();
var xpath = $"//*[translate(#Name, '{uc}', '{lc}') = '{lc}']";
// -> "//*[translate(#Name, 'ASDASD', 'asdasd') = 'asdasd']"

Can we give null values or empty strings in the input fields for UI Automation?

I am new to karate Automation, testing simple login window where need is to validate the blank fields. When I am inserting empty string " " in input function it's failing the step. How we can achieve this?
* input("//input[#name='password']", " ")
I want to validate the error message which appears when user leaves password field as blank.
Thanks in advance!
The below works fine for me. So maybe you are missing something. A lot depends on the page, maybe there is some JS validation.
* driver 'https://github.com/login'
* input('#login_field', ' ')
* input('#password', ' ')
* submit().click("input[name=commit]")
* match html('#js-flash-container') contains 'Incorrect username or password.'
If still stuck, please follow this process: https://github.com/intuit/karate/tree/develop/examples/ui-test

How to display multiplication result in a textbox using Selenium WebDriver?

I am a manual tester and want to switch to automation testing. I've learnt Selenium WebDriver recently. While practicing, I came across a webpage where I was asked to automate the following thing in a web form:
In the form, they have provided two double numbers and have asked us to display the multiplication of thosenumbers in the textbox. Please guide me how can I display the result in the textbox using selenium webdriver.
Screenshot of the xpath
Add screen shot using below steps-
Click "Edit" to your question.
On upper side you can find Image icon or press "Ctrl + G".
Drag & drop the image or attach by providing the path.
[Note: I have added this as an answer because I don't have 50 points yet to add a comment. Forgive me for that.. :)]
If the two double numbers are in separate input field (any other place except input filed) get the data as follows:
double number01 = Double.parseDouble(driver.findElement(By.cssSelector("your selector for element")).getText());
double number02 = Double.parseDouble(driver.findElement(By.cssSelector("your selector for element2")).getText());
Then perform multiplication and use sendkeys to input into the output field:
driver.findElement(By.cssSelector("your selector for element2")).sendKeys((number01 * number02) + "");
Note:
Above answer is based on assumption according to your question.
Edit:
String terms = driver.findElement(By.cssSelector(("#form > form > label:nth-child(89)"))).getText().replaceAll("=", "");//get the expression for multiplication and remove the '=' sign at the end
String [] temp_xy = terms.split("X"); // split the string into multiplicand and multiplier parts
double multiplicand = Double.parseDouble(temp_xy[0].trim());
double multiplier = Double.parseDouble(temp_xy[1].trim());
double product = multiplicand * multiplier;
Now, you put your product in the desired filed. Similarly, you can find the quotient.