使用WebDriver和Junit 4.11,我有一个滑块元素(将组件的不透明度从0-100%更改)我想要clickAndHold,然后在某个点释放。
我能够将组件拖放到目标区域,然后通过查找x和y轴的文本框元素来更改值。但在这种情况下,我需要自动化滑块。这甚至可能吗?
滑块下面的HTML代码:
<div class="inputWrapper"> <input type="range" value="1" name="opacity" id="c409" min="0" max="1" step="0.01" class="t-textInput-A"><span class="units">%</span><div class="sliderTooltip" style="display: none; margin-left: 97px;">100%</div> </div>
滑块当前设置为100%,但是如何抓取元素并将其拖放到例如10%?
我拍了一张截图来说明我试图抓住的内容:http://screencast.com/t/YjdoFtSG
非常感谢提前。
答案 0 :(得分:1)
这很简单。将值发送到字段并触发&#39;更改&#39;在这个领域的事件。 例如,在浏览器中执行此脚本:
arguments[0].valueAsNumber = arguments[1];
var mEvent = document.createEvent("Event");
mEvent.initEvent('change', true, true);
arguments[0].dispatchEvent(mEvent);
argument[0]
- 输入类型=范围网络元素argument[1]
- 所需尺寸注意,此示例适用于Firefox和Chrome,但不适用于Internet Explorer。阅读有关&#34; fireEvent&#34;的文档在IE中。
答案 1 :(得分:0)
你可以尝试尝试这些功能(我使用的是phpwebdriver)
/**
* Move the mouse by an offset of the specificed element.
* If no element is specified, the move is relative to the current mouse cursor.
* If an element is provided but no offset, the mouse will be moved to the center of the element.
* If the element is not visible, it will be scrolled into view.
*
* @param WebElement $element
* ID of the element to move to. If not specified or is null,
* the offset is relative to current position of the mouse.
* @param integer $xoffset
* X offset to move to, relative to the top-left corner of the element.
* If not specified, the mouse will move to the middle of the element.
* @param integer $yoffset
* Y offset to move to, relative to the top-left corner of the element.
* If not specified, the mouse will move to the middle of the element.
*/
public function moveTo($element = null, $xoffset = null, $yoffset = null)
{
$request = $this->requestURL . "/moveto";
$session = $this->curlInit($request);
$array = explode('/', $element->requestURL);
$id = $array[count($array) - 1];
$args = array();
if($element) $args['element'] = $id;
if($xoffset) $args['xoffset'] = intval($xoffset);
if($yoffset) $args['yoffset'] = intval($yoffset);
if(empty($args))
$this->preparePOST($session, null);
else{
$postargs = json_encode($args);
$this->preparePOST($session, $postargs);
}
curl_exec($session);
}
/**
* Click and hold the left mouse button (at the coordinates set by the last moveto command).
* Note that the next mouse-related command that should follow is buttonUp() .
* Any other mouse command (such as click() or another call to buttonDown()) will yield undefined behaviour.
*
* @param integer $button
* Which button, enum: {LEFT = 0, MIDDLE = 1 , RIGHT = 2}.
* Defaults to the left mouse button if not specified.
*/
public function buttonDown()
{
$request = $this->requestURL . "/buttondown";
$session = $this->curlInit($request);
$this->preparePOST($session, null);
curl_exec($session);
}
/**
* Releases the mouse button previously held (where the mouse is currently at).
* Must be called once for every buttonDown() command issued.
* See the note in click and buttonDown() about implications of out-of-order commands.
*
* @param integer $button
* Which button, enum: {LEFT = 0, MIDDLE = 1 , RIGHT = 2}.
* Defaults to the left mouse button if not specified.
*/
public function buttonUp()
{
$request = $this->requestURL . "/buttonup";
$session = $this->curlInit($request);
$this->preparePOST($session, null);
curl_exec($session);
}
/**
* Maximize the specified window if not already maximized.
* If the :windowHandle URL parameter is "current", the currently active window will be maximized.
*/
答案 2 :(得分:0)
在这种情况下,我通常不得不通过坐标。如果你正在使用clickAndHold我假设你正在将动作串联起来,那么对于第二个动作,请执行:
moveByOffset(-100,0)
或任何正确的移动大小。这将使鼠标从其当前位置(在clickAndHold之后)移动100px,这将拖动滑块。