I've a scenario where I need to automate the payment page.
I need to enter the credit card details, but the thing is when I use sendkeys method to type the text, it doesn't take the full card number though it only takes the first 4 chars.
The card field is designed in such a way that it takes 4 numbers and then provide auto space and then next 4 numbers and so on...
How would I handle this particular thing?
You can use this as this code sends the character one by one and I hope that the payment gateway will accept the cardnumber if the characters are sent one by one.
for(int i=0;i<ccNumber.length();i++){
creditcardtextbox.sendKeys(ccNumber.substring(i).split(""));
}
I'm having the same issue, using Python and Selenium and the credit card field only takes 4 characters
Here is the code
#move to the Shipping screen
elem.send_keys(Keys.RETURN)
#move to the Payment, no return key here, press the button
driver.implicitly_wait(1) # seconds
driver.find_element_by_name("button").click()
#enter the payment details
driver.implicitly_wait(1) # seconds
elem = driver.find_element_by_id("checkout_credit_card_number")
elem.send_keys(str(sys.argv[5]), Keys.ARROW_DOWN)
elem = driver.find_element_by_id("checkout_credit_card_name")
elem.send_keys(str(sys.argv[6]))
elem = driver.find_element_by_id("checkout_credit_card_month")
elem.send_keys(str(sys.argv[7]))
elem = driver.find_element_by_id("checkout_credit_card_year")
elem.send_keys(str(sys.argv[8]))
elem = driver.find_element_by_id("checkout_credit_card_verification_value")
elem.send_keys(str(sys.argv[9]), Keys.ARROW_DOWN)
You can do this by :
driver.find_element_by_id("checkout_credit_card_number").sendKeys("5123");
driver.find_element_by_id("checkout_credit_card_number").sendKeys("4567");
driver.find_element_by_id("checkout_credit_card_number").sendKeys("8901");
driver.find_element_by_id("checkout_credit_card_number").sendKeys("2346");
Here's my solution mocked in Python:
from selenium.webdriver.common.by import By
ccn = '4111111111111111'
ccn_locator = driver.find_element(By.ID, 'numberField')
for num in ccn.replace('', ' ').strip().split():
ccn_locator.send_keys(num)
You can do like below to handle space between every 4 digits :
driver.findElement(By.id("elementid")).sendKeys("1");
driver.findElement(By.id("elementid")).sendKeys("2");
driver.findElement(By.id("elementid")).sendKeys("3");
driver.findElement(By.id("elementid")).sendKeys("5");
driver.findElement(By.id("elementid")).sendKeys(Keys.SPACE);
driver.findElement(By.id("elementid")).sendKeys("5");
driver.findElement(By.id("elementid")).sendKeys("1");
driver.findElement(By.id("elementid")).sendKeys("2");
driver.findElement(By.id("elementid")).sendKeys("3");
driver.findElement(By.id("elementid")).sendKeys(Keys.SPACE);
Above is sample code , please replace your actual element ID and digits. Also I have given just sample for 8 digits , you can do same for 16 digits. Just call that SPACE key after every 4 digits. Your elementid will be same for all lines.
Note : There may be other way to do above but I have tried above and working fine so suggesting you same.
Related
I'd like to modify part of the text in a textarea with Selenium. The textarea seems almost as if it were read-only.
In this very simple example using a sample algo, it would be great to be able to change the stock id on this line:
context.aapl = sid(24)
... to something like:
context.aapl = sid(39840)
... which is the Tesla stock id. The variable name will no longer make sense, doesn't matter, just a start.
This Selenium code for me is able to open the sample with no login required.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
t = webdriver.Firefox() # t stands for tab as in browser tab in my mind
t.implicitly_wait(10)
t.get('https://www.quantopian.com/algorithms/')
o = t.find_element_by_xpath("//body") # o stands for object
o.send_keys(Keys.ESCAPE) # clearing the popup
o = t.find_element_by_link_text("Hello World Algorithm")
o.click()
''' for the fun of it if you want to run the backtest
o = t.find_element_by_xpath('//body')
o.send_keys(Keys.CONTROL + 'b')
o.send_keys(Keys.ESCAPE)
'''
print t.find_element_by_id('code-area').text
Here's the output from that
1
# Called once at the start of the simulation.
2
def initialize(context):
3
# Reference to the AAPL security.
4
context.aapl = sid(24)
5
6
# Rebalance every day, one hour and a half after market open.
7
schedule_function(my_rebalance,
8
date_rules.every_day(),
9
time_rules.market_open(hours=1, minutes=30))
10
11
# This function was scheduled to run once per day at 11AM ET.
12
def my_rebalance(context, data):
13
14
# Take a 100% long position in AAPL. Readjusts each day to
15
# account for price fluctuations.
16
if data.can_trade(context.aapl):
17
order_target_percent(context.aapl, 1.00)
That id is 'code-area'. The content includes margin numbers which might be a problem.
Next nested area is 'code-area-internal', seems the same.
Followed by these two.
<div class='ide-container' id='ide-container'>
<textarea class='width_100pct' id='codebox'>
In trying to obtain the content of the algorithm with 'codebox', content doesn't appear to be present, just u'' ...
>>> p = t.find_element_by_id('codebox').text
>>> p
u''
Attempt to do CTRL-A on it results in this exception...
>>> o = t.find_element_by_id('codebox')
>>> o.send_keys(Keys.CONTROL + 'a')
ElementNotInteractableException: Message: Element is not reachable by keyboard
If the text can be completely cut, then replace can done in Python and paste, that would be fine.
I wouldn't expect Selenium to be able to find and replace text, just surprised it finds a visible area for user input to be off limits from interactivity.
That textarea does have its own Find, and hoping won't have to resort to trying to use it as a workaround.
(The environment is an online IDE for stock market algorithms called Quantopian)
This is the one other thing I tried, with no apparent effect:
>>> t.execute_script("arguments[0].value = arguments[1]", t.find_element_by_id("ide-container"), "_new_")
Appreciate any pointers.
Textarea has style="display: none" attribute which means that you cannot get its content with text property. In this case you can use:
p = t.find_element_by_id('codebox').get_attribute("textContent")
To set new value to code field you can use:
field = driver.find_element_by_css_selector('div[role="presentation"]')
driver.execute_script("arguments[0].textContent = 'New value';", field)
But note that initially each code line in code field displayed as separate div node with specific value and styles. So to make new value looks exactly as code (in the same formatting) you can prepare HTML sample e.g.
value = """<div style="position: relative;"><div class="CodeMirror-gutter-wrapper" style="left: -48px;"><div class="CodeMirror-linenumber CodeMirror-gutter-elt" style="left: 15px; width: 21px;">1</div></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># Comment for new code.</span></span></pre></div>"""
and do
driver.execute_script("arguments[0].innerHTML = arguments[1];", field, value)
The content of the algorithm with codebox which you are trying to extract is having the style attribute set to display: none;. So to extract the text you can use the following lines of code :
p = t.find_element_by_xpath("//div[#class='ide-container']/textarea[#id='codebox']")
t.execute_script("arguments[0].removeAttribute('style')", p)
print(t.get_attribute("innerHTML"))
I am quite new to robot, and have only been working solo on it at work for a month or so.
Currently I am trying to count the total number on rows in a table within the application I am testing. (chrome based)
This is what I am using:
${count}= get element count //table[#class='options-table']/tbody/tr
Which brings back a value of 5 - this is counting the first page. However, I'm expecting it to bring back 76 as there are multiple pages.
Can anyone help on how to bring back the amount of rows across multiple pages?
${count}= get element count //table[#class='options-table']/tbody/tr
Expected result: 76
Actual result: 5 (only the first page)
To avoid a slightly complex logic (iterating through pages, summing up element counts) in a Robot Framework keyword you could write your own keyword in Python for example.
In this case you need a keyword that takes an element locator (//table[#class='options-table']/tbody/tr to be specific) and a list of page urls.
To implement such keyword, create a file like ExtendedSeleniumLib.py:
from robot.libraries.BuiltIn import BuiltIn
def get_element_count_from_pages(locator, *page_urls):
seleniumlib = BuiltIn().get_library_instance('SeleniumLibrary')
element_count = 0
for url in page_urls:
seleniumlib.go_to(url)
element_count += seleniumlib.get_element_count(locator)
return element_count
and from your test code you can use it like:
*** Settings ***
Library SeleniumLibrary
Library ExtendedSeleniumLib
*** Variables ***
${SE HEADER LOCATOR} //a[#class='site-header--link fs-headline1 fw-bold']
*** Test Cases ***
Count Elements On Multiple Pages Example
[Setup] Open Browser https://stackoverflow.com Firefox
Maximize Browser Window
Set Selenium Speed 0.1
${count}= Get Element Count From Pages ${SE HEADER LOCATOR}
... https://iot.stackexchange.com/
... https://sqa.stackexchange.com/
... https://robotics.stackexchange.com/
Should Be Equal As Integers ${count} 3
[Teardown] Close Browser
This example iterates through three Stack Exchange sites and counts the header elements. As there should be only one on each page the expected result is 3. Based on this you should be able to count the table rows on your pages.
About how to configure search path for libraries and resources, check the relevant chapter form the Robot Framework User Guide; Configuring where to search libraries and other extensions. If you place the python file into the same directory where your robot file is, then you do not need anything to do.
Please check below code, it assumes that the total number of pages will not be more than 100 since I'm not aware of the webpage, you can either take this number from webpage if available. Also, if you are sure that total number of rows per page is always 5 then you can use below formula
[ 5 * (total number of pages - 1 ) + row count of the last page]
This can give you total row count across all pages without traversing through all the pages. Also, please add any time synchronisation steps for the successful run.
Get Count of All Pages
${next_page_locator} Set Variable enter next page icon/link xpath here
${first_row_locator} Set Variable enter first row xpath here
${total_count} set variable 0
: FOR ${index} IN RANGE 1 100
\ Wait Until Element Is Visible ${first_row_locator}
\ ${count} get element count //table[#class='options-table']/tbody/tr
\ ${total_count} evaluate ${count} + ${total_count}
\ ${next_link_present} Run Keyword And Return Status Page Should Contain Element ${next_page_locator}
\ exit for loop if ${next_link_present} is ${False}
\ Click Element ${next_page_locator}
I have two input fields to enter choices which have same class, type. Id is different by it is dynamic and create on run time so i can't use id.I used indexing ,it's not working properly.
driver.findElement(By.xpath("//input[#type='text'][#placeholder='Provide a response entry that customers can select'][1]")).click();
driver.findElement(By.xpath("//input[#type='text'][#placeholder='Provide a response entry that customers can select'][1]")).sendKeys("Iphone 6");
driver.findElement(By.xpath("//input[#type='text'][#placeholder='Provide a response entry that customers can select'][2]")).click();
driver.findElement(By.xpath("//input[#type='text'][#placeholder='Provide a response entry that customers can select'][2]")).sendKeys("Iphone 7");
I used indexing in given image link.
click link to view code in organized way
Index 1 works in this case but unable to find index 2.
Given inspected html code is below of input field 1 and field 2
Field 1
Input field 1 image Xpath link
field 2
Input field 2 image link
If these two inputs are always in this sequence (so the first input is always first and second always second)
You can use:
driver.findElement(By.xpath("(//input[#type='text'][#placeholder='Provide a response entry that customers can select'])[1]")).click();
driver.findElement(By.xpath("(//input[#type='text'][#placeholder='Provide a response entry that customers can select'])[2]")).click();
At the same time I have corrected the syntax in indexing
Building on #Anand 's answer, you can simplify a little:
WebElement button1 = driver.findElement(By.xpath("(//input[#type='text' and #placeholder='Provide a response entry that customers can select'])[1]"));
WebElement button2 = driver.findElement(By.xpath("(//input[#type='text' and #placeholder='Provide a response entry that customers can select'])[2]"));
I think it's a little easier to read using and instead of stacking brackets.
I use it similarly for widgets:
WebElement header = driver.findElement(By.xpath("//div[contains(#class,'panel')]/div[contains(#class,'panel-heading') and text()[contains(.,'News Feed')]]"));
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.
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.