I am writing an automation test script using Robot Framework & Selenium2Library for testing our web application (I am writing test cases in .txt
format)
I am having a problem on handling two different input fields using Robot Framework. I have to change the values of these two input types using RF. So far I couldn't find any specific keywords to perform this functionalities.
These are the different input fields I have to test:
- Range Input Field
html code : <input type="range" id="fontSize" min="8" max="20" step="1" value="12">
Here is the testing scenario : I have to move the slider to a different location & I have to check the value of input range field
- Color Picker
Here is the testing scenario: I have to move the pointer through out the color picker window & Select a particular color from the color picker ( I have to store the selected color into a variable for future reference)
I didn't get any particular solution for handling this issue. Is there any keywords in robot framework to test 'Range Input Field' & 'Color Picker'? Can you please suggest asolution for this issue
-
Can you point to a publicly available sample of the controls, so we can see and try possible solutions? Apart from that, yes, robotframework/Selenium don't have such high-level keywords and methods. I doubt any generic framework does - they have to be constructed for the particular SUT, according to the functionality it provides.Todor Minakov– Todor Minakov2017年03月16日 07:15:33 +00:00Commented Mar 16, 2017 at 7:15
-
I'm having the same problem, have you found a way to solve itGonzalo Parra– Gonzalo Parra2017年04月19日 13:14:20 +00:00Commented Apr 19, 2017 at 13:14
1 Answer 1
For input[type="range"]
, following keyword Set Range
can be used. I have tested this with this HTML and the used the attached test case for testing the keyword. Just add the following code snippet in Selenium2Library/keyword/_element.py
and import math
, time library.
import math
import time
def set_range(self, locator, val):
# The adjustment helper to drag the slider thumb
def adjust(deltax):
if deltax < 0:
deltax = int(math.floor(min(-1, deltax)))
else:
deltax = int(math.ceil(max(1, deltax)))
ac = ActionChains(self._current_browser())
ac.click_and_hold(None)
ac.move_by_offset(deltax, 0)
ac.release(None)
ac.perform()
el = self._element_find(locator, True, True)
minval = float(el.get_attribute("min") or 0)
maxval = float(el.get_attribute("max") or 100)
v = max(0, min(1, (float(val) - minval) / (maxval - minval)))
width = el.size["width"]
target = float(width) * v
ac = ActionChains(self._current_browser())
# drag from min to max value, to ensure oninput event
ac.move_to_element_with_offset(el, 0, 1)
ac.click_and_hold()
ac.move_by_offset(width, 0)
# drag to the calculated position
ac.move_to_element_with_offset(el, target, 1)
ac.release()
ac.perform()
# perform a binary search and adjust the slider thumb until the value matches
minguess = 0
maxguess = 0
while True:
curval = el.get_attribute("value")
if float(curval) == float(val):
return True
prev_guess = target
if float(curval) < float(val):
minguess = target
target += (maxguess - target) / 2
else:
maxguess = target
target = minguess + (target - minguess) / 2
deltax = target-prev_guess
if abs(deltax) < 0.5:
break # cannot find a way, fallback to javascript.
time.sleep(0.1) # Don't consume CPU too much
adjust(deltax)
# Finally, if the binary search algoritm fails to achieve the final value
# we'll revert to the javascript method so at least the value will be changed
# even though the browser events wont' be triggered.
# Fallback
self._current_browser().execute_script("arguments[0].value=arguments[1];", el, val)
curval = el.get_attribute("value")
if float(curval) == float(val):
return True
else:
raise Exception("Can't set value %f for the element." % val)
Here's the test case:
*** Settings ***
Documentation TC1: A test case to set range
Resource common_resource.txt
*** Test Cases ***
A test case to set range
Open Browser url=https://www.w3schools.com/code/tryit.asp?filename=FEXUIFH94YGB
Click Element css=button[onclick="submitTryit(1)"]
Select Frame css=iframe#iframeResult
Wait Until Page Contains Element css=input[type="range"]
Set Range css=input[type="range"] 8
Set Range css=input[type="range"] 20
Reference: Selenium: How to automate html5 input range element on Webdriver
Explore related questions
See similar questions with these tags.