Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement tag name selector for FindElementsFromElement WebDriver com…
…mand
  • Loading branch information
georgeroman committed Jul 21, 2019
1 parent 616a81f commit bf6ea64
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 27 deletions.
9 changes: 9 additions & 0 deletions components/script/script_thread.rs
Expand Up @@ -2070,6 +2070,15 @@ impl ScriptThread {
reply,
)
},
WebDriverScriptCommand::FindElementElementsTagName(selector, element_id, reply) => {
webdriver_handlers::handle_find_element_elements_tag_name(
&*documents,
pipeline_id,
element_id,
selector,
reply,
)
},
WebDriverScriptCommand::FocusElement(element_id, reply) => {
webdriver_handlers::handle_focus_element(
&*documents,
Expand Down
26 changes: 24 additions & 2 deletions components/script/webdriver_handlers.rs
Expand Up @@ -284,7 +284,7 @@ pub fn handle_find_element_elements_css(
pipeline: PipelineId,
element_id: String,
selector: String,
reply: IpcSender<Result<Option<String>, ()>>,
reply: IpcSender<Result<Vec<String>, ()>>,
) {
let node_ids = find_node_by_unique_id(documents, pipeline, element_id)
.ok_or(())
Expand All @@ -295,12 +295,34 @@ pub fn handle_find_element_elements_css(
.map(|nodes| {
nodes
.iter()
.map(|x| Some(x.upcast::<Node>().unique_id()))
.map(|x| x.upcast::<Node>().unique_id())
.collect()
});
reply.send(node_ids).unwrap();
}

pub fn handle_find_element_elements_tag_name(
documents: &Documents,
pipeline: PipelineId,
element_id: String,
selector: String,
reply: IpcSender<Result<Vec<String>, ()>>,
) {
let node_ids = find_node_by_unique_id(documents, pipeline, element_id)
.ok_or(())
.and_then(|node| match node.downcast::<Element>() {
Some(elem) => Ok(elem.GetElementsByTagName(DOMString::from(selector))),
None => Err(()),
})
.map(|nodes| {
nodes
.elements_iter()
.map(|x| x.upcast::<Node>().unique_id())
.collect::<Vec<String>>()
});
reply.send(node_ids).unwrap();
}

pub fn handle_focus_element(
documents: &Documents,
pipeline: PipelineId,
Expand Down
3 changes: 2 additions & 1 deletion components/script_traits/webdriver_msg.rs
Expand Up @@ -30,7 +30,8 @@ pub enum WebDriverScriptCommand {
FindElementsTagName(String, IpcSender<Result<Vec<String>, ()>>),
FindElementElementCSS(String, String, IpcSender<Result<Option<String>, ()>>),
FindElementElementTagName(String, String, IpcSender<Result<Option<String>, ()>>),
FindElementElementsCSS(String, String, IpcSender<Result<Option<String>, ()>>),
FindElementElementsCSS(String, String, IpcSender<Result<Vec<String>, ()>>),
FindElementElementsTagName(String, String, IpcSender<Result<Vec<String>, ()>>),
FocusElement(String, IpcSender<Result<(), ()>>),
GetActiveElement(IpcSender<Option<String>>),
GetCookie(String, IpcSender<Vec<Serde<Cookie<'static>>>>),
Expand Down
48 changes: 30 additions & 18 deletions components/webdriver_server/lib.rs
Expand Up @@ -997,34 +997,46 @@ impl Handler {
element: &WebElement,
parameters: &LocatorParameters,
) -> WebDriverResult<WebDriverResponse> {
if parameters.using != LocatorStrategy::CSSSelector {
return Err(WebDriverError::new(
ErrorStatus::UnsupportedOperation,
"Unsupported locator strategy",
));
}

let (sender, receiver) = ipc::channel().unwrap();
let cmd = WebDriverScriptCommand::FindElementElementsCSS(
parameters.value.clone(),
element.id.clone(),
sender,
);

self.browsing_context_script_command(cmd)?;
match parameters.using {
LocatorStrategy::CSSSelector => {
let cmd = WebDriverScriptCommand::FindElementElementsCSS(
parameters.value.clone(),
element.id.clone(),
sender,
);
self.browsing_context_script_command(cmd)?;
},
LocatorStrategy::TagName => {
let cmd = WebDriverScriptCommand::FindElementElementsTagName(
parameters.value.clone(),
element.id.clone(),
sender,
);
self.browsing_context_script_command(cmd)?;
},
_ => {
return Err(WebDriverError::new(
ErrorStatus::UnsupportedOperation,
"Unsupported locator strategy",
));
},
}

match receiver.recv().unwrap() {
Ok(value) => {
let value_resp = value
let resp_value: Vec<Value> = value
.into_iter()
.map(|x| serde_json::to_value(WebElement::new(x)).unwrap())
.collect::<Vec<Value>>();
let value_resp = serde_json::Value::Array(value_resp);
Ok(WebDriverResponse::Generic(ValueResponse(value_resp)))
.collect();
Ok(WebDriverResponse::Generic(ValueResponse(
serde_json::to_value(resp_value)?,
)))
},
Err(_) => Err(WebDriverError::new(
ErrorStatus::InvalidSelector,
"Invalid Selector",
"Invalid selector",
)),
}
}
Expand Down
Expand Up @@ -44,18 +44,12 @@
[test_parent_of_document_node_errors]
expected: FAIL

[test_no_element[css selector-#wontExist\]]
expected: FAIL

[test_find_elements_partial_link_text[<a href=#>partial link&amp;text</a>-k&t\]]
expected: FAIL

[test_find_elements[xpath-//a\]]
expected: FAIL

[test_find_elements[tag name-a\]]
expected: FAIL

[test_find_elements_partial_link_text[<a href=# style='text-transform: uppercase'>partial link text</a>-LINK\]]
expected: FAIL

Expand Down

0 comments on commit bf6ea64

Please sign in to comment.