Added new selenium locator examples.

Fixed find_elements in WEB_DRIVER.
This commit is contained in:
jvelilla
2013-05-18 23:12:37 -03:00
parent 7679898159
commit ad5b52f4e4
13 changed files with 428 additions and 43 deletions

View File

@@ -30,4 +30,28 @@
<root class="FIND_ELEMENT_CLASS" feature="default_create"/>
<cluster name="src" location=".\" recursive="true"/>
</target>
<target name="findElementChild" extends="selenium_example">
<root class="FIND_ELEMENT_CHILD" feature="default_create"/>
<cluster name="src" location=".\" recursive="true"/>
</target>
<target name="findElementsLinks" extends="selenium_example">
<root class="FIND_ELEMENTS_LINKS" feature="default_create"/>
<cluster name="src" location=".\" recursive="true"/>
</target>
<target name="findElementsLinksByText" extends="selenium_example">
<root class="FIND_ELEMENTS_LINKS_BY_TEXT" feature="default_create"/>
<cluster name="src" location=".\" recursive="true"/>
</target>
<target name="findElementsLinksByPartialText" extends="selenium_example">
<root class="FIND_ELEMENTS_LINKS_BY_PARTIAL_TEXT" feature="default_create"/>
<cluster name="src" location=".\" recursive="true"/>
</target>
<target name="findElementCssSelector" extends="selenium_example">
<root class="FIND_ELEMENT_CSS_SELECTOR" feature="default_create"/>
<cluster name="src" location=".\" recursive="true"/>
</target>
</system>

View File

@@ -0,0 +1,61 @@
note
description: "The WEB_ELEMENT class also supports find methods that find child elements."
class
FIND_ELEMENT_CHILD
inherit
ANY
redefine
default_create
end
create
default_create
feature
default_create
do
search
end
feature -- Search by id
search
local
web_driver: WEB_DRIVER
wait: WEB_DRIVER_WAIT
do
--Create a new instance of a Web driver
create web_driver.make
-- Start session with chrome
web_driver.start_session_chrome
-- Go to EiffelRoom login page
web_driver.to_url ("http://www.eiffelroom.com/")
-- Find the element div with id page,and then we can find a child element div page-inner
--<div id="page">
-- <div id="page-inner">
-- <a id="navigation-top" name="top"></a>
-- <div id="skip-to-nav">
-- <div id="header">
-- <div id="main">
-- <div id="footer">
-- </div>
--</div>
if attached {WEB_ELEMENT} web_driver.find_element ((create {SE_BY}).id ("page")) as l_div_page then
print ("%N Page element id" + l_div_page.element)
if attached {WEB_ELEMENT}l_div_page.find_element ((create {SE_BY}).id ("page-inner")) as l_div_page_inner then
print ("%N Inner Page element id" + l_div_page_inner.element)
end
end
-- close the window
web_driver.window_close
print ("%N")
end
end

View File

@@ -0,0 +1,66 @@
note
description: "[
]"
class
FIND_ELEMENT_CSS_SELECTOR
inherit
ANY
redefine
default_create
end
create
default_create
feature
default_create
do
search
end
feature -- Search by id
search
local
web_driver: WEB_DRIVER
do
--Create a new instance of a Web driver
create web_driver.make
-- Start session with chrome
web_driver.start_session_chrome
-- Go to EiffelRoom home page
web_driver.to_url ("http://www.eiffelroom.com/")
-- Absolute Path
-- <div id="header">
-- html.js body.front div#page div#page-inner div#header
if attached {WEB_ELEMENT} web_driver.find_element ((create {SE_BY}).css_selector ("html.js body.front div#page div#page-inner div#header")) as selector then
print ("%NElement:" + selector.element)
end
-- Relative Path
--
if attached {WEB_ELEMENT} web_driver.find_element ((create {SE_BY}).css_selector ("div#header")) as selector then
print ("%NElement:" + selector.element)
end
print ("%Nend process ...")
io.read_line
-- close the window
web_driver.window_close
end
expected_title (driver: WEB_DRIVER; title: STRING_32): BOOLEAN
do
if attached {STRING_32} driver.get_page_tile as l_title and then l_title.has_substring (title) then
Result := True
end
end
end

View File

@@ -0,0 +1,65 @@
note
description: "[
]"
class
FIND_ELEMENTS_LINKS
inherit
ANY
redefine
default_create
end
create
default_create
feature
default_create
do
search
end
feature -- Search by id
search
local
web_driver: WEB_DRIVER
wait: WEB_DRIVER_WAIT
do
--Create a new instance of a Web driver
create web_driver.make
-- Start session with chrome
web_driver.start_session_chrome
-- Go to EiffelRoom home page
web_driver.to_url ("http://www.eiffelroom.com/")
create wait.make (web_driver,10)
wait.until_when (agent expected_title (web_driver, "Eiffel Room"))
-- Find links
if attached {ARRAYED_LIST[WEB_ELEMENT]}web_driver.find_elements ((create {SE_BY}).tag_name("a")) as l_links then
from
l_links.start
until
l_links.after
loop
if attached l_links.item.get_attribute ("href") as l_ref then
print ("%Nhref:" + l_ref)
end
l_links.forth
end
end
print ("%Nend process ..." )
io.read_line
-- close the window
web_driver.window_close
end
end

View File

@@ -0,0 +1,71 @@
note
description: "[
]"
class
FIND_ELEMENTS_LINKS_BY_PARTIAL_TEXT
inherit
ANY
redefine
default_create
end
create
default_create
feature
default_create
do
search
end
feature -- Search by id
search
local
web_driver: WEB_DRIVER
wait: WEB_DRIVER_WAIT
do
--Create a new instance of a Web driver
create web_driver.make
-- Start session with chrome
web_driver.start_session_chrome
-- Go to EiffelRoom home page
web_driver.to_url ("http://www.eiffelroom.com/")
-- Find links
-- <a href="http://www.eiffel.com">Eiffel.com</a></li><li><a>
if attached {WEB_ELEMENT} web_driver.find_element ((create {SE_BY}).partial_link_text("Eiffel")) as l_link then
if attached l_link.get_attribute ("href") as l_ref then
print ("%Nhref:" + l_ref)
end
end
-- <a href="#navigation">Skip to Navigation</a>
if attached {WEB_ELEMENT} web_driver.find_element ((create {SE_BY}).partial_link_text("Skip to")) as l_link then
if attached l_link.get_attribute ("href") as l_ref then
print ("%Nhref:" + l_ref)
end
end
print ("%Nend process ..." )
io.read_line
-- close the window
web_driver.window_close
end
expected_title (driver : WEB_DRIVER; title : STRING_32) : BOOLEAN
do
if attached {STRING_32} driver.get_page_tile as l_title and then l_title.has_substring (title) then
Result := True
end
end
end

View File

@@ -0,0 +1,55 @@
note
description: "[
]"
class
FIND_ELEMENTS_LINKS_BY_TEXT
inherit
ANY
redefine
default_create
end
create
default_create
feature
default_create
do
search
end
feature -- Search by id
search
local
web_driver: WEB_DRIVER
wait: WEB_DRIVER_WAIT
do
--Create a new instance of a Web driver
create web_driver.make
-- Start session with chrome
web_driver.start_session_chrome
-- Go to EiffelRoom home page
web_driver.to_url ("http://www.eiffelroom.com/")
-- Find links
-- <a href="http://www.eiffel.com">Eiffel.com</a></li><li><a>
if attached {WEB_ELEMENT} web_driver.find_element ((create {SE_BY}).link_text("Eiffel.com")) as l_link then
if attached l_link.get_attribute ("href") as l_ref then
print ("%Nhref:" + l_ref)
end
end
print ("%Nend process ..." )
io.read_line
-- close the window
web_driver.window_close
end
end

View File

@@ -0,0 +1,6 @@
#include <windows.h>
STRINGTABLE
BEGIN
1 "This Program was made using EiffelStudio using Visual Studio C++"
END

View File

@@ -0,0 +1,6 @@
#include <windows.h>
STRINGTABLE
BEGIN
1 "This Program was made using EiffelStudio using Visual Studio C++"
END

View File

@@ -64,7 +64,7 @@ feature -- Commands
-- TODO move this scenario to feature new_response
if not (resp.status >= 400) then
if attached resp.header ("Location") as l_location then
resp := http_new_session (l_location).get ("", context_executor)
resp := http_session.get (l_location.substring ((host.count+1),l_location.count), context_executor)
Result := new_response ("", resp)
end
else

View File

@@ -995,8 +995,8 @@ feature -- Commands
until
index > l_json_array.count
loop
if attached {JSON_OBJECT} l_json_array.i_th (index) as json_str and then attached json_str.item ("ELEMENT") as l_elem then
Result.force (create {WEB_ELEMENT}.make (l_elem.representation, Current, a_session_id))
if attached {JSON_OBJECT} l_json_array.i_th (index) as json_str and then attached {JSON_STRING} json_str.item ("ELEMENT") as l_elem then
Result.force (create {WEB_ELEMENT}.make (l_elem.item, Current, a_session_id))
end
index := index + 1
end

View File

@@ -7,17 +7,21 @@ note
class
WEB_DRIVER
inherit
EXCEPTIONS
create
make, make_with_host
feature -- Initialization
feature -- Initialization
make
do
create api.make
end
make_with_host (a_host : STRING)
make_with_host (a_host: STRING)
do
create api.make_with_host (a_host)
end
@@ -43,19 +47,46 @@ feature -- Initialize Session
start_session_chrome
local
l_capabilities: SE_CAPABILITIES
env: EXECUTION_ENVIRONMENT
retried: BOOLEAN
do
create l_capabilities.make
l_capabilities.set_browser_name ("chrome")
if attached api.create_session_with_desired_capabilities (l_capabilities) as l_session then
session := l_session
if not retried then
session := api.create_session_with_desired_capabilities (l_capabilities)
if not is_session_active then
raise ("Session not active")
end
else
session := api.create_session_with_desired_capabilities (l_capabilities)
end
rescue
retried := true
create env
env.sleep (1000)
retry
end
start_session_chrome_with_desired_capabilities (a_desired_capabilities: SE_CAPABILITIES)
require
browser_name_chrome: attached a_desired_capabilities.browser_name as l_browser_name and then l_browser_name ~ "chrome"
local
env: EXECUTION_ENVIRONMENT
retried: BOOLEAN
do
session := api.create_session_with_desired_capabilities (a_desired_capabilities)
if not retried then
session := api.create_session_with_desired_capabilities (a_desired_capabilities)
if not is_session_active then
raise ("Session not active")
end
else
session := api.create_session_with_desired_capabilities (a_desired_capabilities)
end
rescue
retried := true
create env
env.sleep (1000)
retry
end
start_session_ie
@@ -366,91 +397,91 @@ feature -- Common
end
end
find_element (by: STRING_32) : detachable WEB_ELEMENT
-- Find the first WebElement using the given strategy.
find_element (by: STRING_32): detachable WEB_ELEMENT
-- Find the first WebElement using the given strategy.
require
exist_session : is_session_active
valid_strategy : (create {SE_BY}).is_valid_strategy (by)
exist_session: is_session_active
valid_strategy: (create {SE_BY}).is_valid_strategy (by)
do
if attached session as l_session then
Result := api.search_element (l_session.session_id, by)
end
end
find_elements (by: STRING_32) : detachable LIST[WEB_ELEMENT]
-- Find all elements within the current page using the given mechanism..
find_elements (by: STRING_32): detachable LIST [WEB_ELEMENT]
-- Find all elements within the current page using the given mechanism..
require
exist_session : is_session_active
valid_strategy : (create {SE_BY}).is_valid_strategy (by)
exist_session: is_session_active
valid_strategy: (create {SE_BY}).is_valid_strategy (by)
do
if attached session as l_session then
Result := api.search_elements (l_session.session_id, by)
end
end
get_current_url : detachable STRING_32
-- Retrieve the URL of the current page.
get_current_url: detachable STRING_32
-- Retrieve the URL of the current page.
require
exist_session : is_session_active
exist_session: is_session_active
do
if attached session as l_session then
Result := api.retrieve_url (l_session.session_id)
end
end
get_page_source : detachable STRING_32
-- Get the current page source.
get_page_source: detachable STRING_32
-- Get the current page source.
require
exist_session : is_session_active
exist_session: is_session_active
do
if attached session as l_session then
Result := api.page_source (l_session.session_id)
end
end
get_page_tile : detachable STRING_32
--Get the current page title
get_page_tile: detachable STRING_32
--Get the current page title
require
exist_session : is_session_active
exist_session: is_session_active
do
if attached session as l_session then
Result := api.page_title (l_session.session_id)
end
end
get_window_handle : detachable STRING_32
get_window_handle: detachable STRING_32
require
exist_session : is_session_active
exist_session: is_session_active
do
if attached session as l_session then
Result := api.retrieve_window_handle (l_session.session_id)
end
end
get_window_handles : detachable LIST[STRING_32]
get_window_handles: detachable LIST [STRING_32]
require
exist_session : is_session_active
exist_session: is_session_active
do
if attached session as l_session then
Result := api.retrieve_window_handles (l_session.session_id)
end
end
feature {WEB_DRIVER, WEB_DRIVER_WAIT}
session_wait (duration : INTEGER_64)
session_wait (duration: INTEGER_64)
do
if attached session as l_session then
api.set_session_timeouts_async_script (l_session.session_id, duration.as_integer_32)
api.set_session_timeouts_async_script (l_session.session_id, duration.as_integer_32)
end
end
feature {NONE} -- Implementation
session: detachable SE_SESSION
api : SE_JSON_WIRE_PROTOCOL
status: BOOLEAN
api: SE_JSON_WIRE_PROTOCOL
end

View File

@@ -46,7 +46,7 @@ feature -- Access
if condition.item([]) then
found := True
end
create l_time2.make_now
l_time2.make_now
end
end

View File

@@ -15,7 +15,7 @@ feature
api := an_api
session_id := a_session_id
end
feature -- Access
element : STRING_32
--The opaque ID assigned to the element by the server.
@@ -40,16 +40,16 @@ feature -- Web Element API
api.element_click (session_id, element)
end
find_element (by : SE_BY) : detachable WEB_ELEMENT
find_element (by : STRING_32) : detachable WEB_ELEMENT
-- Find the first WebElement using the given method.
do
Result := api.search_element (session_id,"")
Result := api.search_element (session_id,by)
end
find_elementS (by : SE_BY) : detachable LIST[WEB_ELEMENT]
find_elements (by : STRING_32) : detachable LIST[WEB_ELEMENT]
-- Find all elements within the current context using the given mechanism.
do
Result := api.search_elements (session_id,"")
Result := api.search_elements (session_id,by)
end
get_attribute (name : STRING_32) : detachable STRING_32