Best Selenium code snippet using Selenium.process
page_object.rb
Source:page_object.rb
...189 # platform method to get the value stored in a text field190 # See PageObject::Accessors#text_field191 #192 def text_field_value_for(identifier)193 process_selenium_call(identifier, Elements::TextField, 'input', :type => 'text') do |how, what|194 @browser.find_element(how, what).attribute('value')195 end196 end197 #198 # platform method to set the value for a text field199 # See PageObject::Accessors#text_field200 #201 def text_field_value_set(identifier, value)202 process_selenium_call(identifier, Elements::TextField, 'input', :type => 'text') do |how, what|203 @browser.find_element(how, what).clear204 @browser.find_element(how, what).send_keys(value)205 end206 end207 #208 # platform method to retrieve a text field element209 # See PageObject::Accessors#text_field210 #211 def text_field_for(identifier)212 find_selenium_element(identifier, Elements::TextField, 'input', :type => 'text')213 end214 #215 # platform method to retrieve all text field elements216 #217 def text_fields_for(identifier)218 find_selenium_elements(identifier, Elements::TextField, 'input', :type => 'text')219 end220 #221 # platform method to get the value stored in a hidden field222 # See PageObject::Accessors#hidden_field223 #224 def hidden_field_value_for(identifier)225 process_selenium_call(identifier, Elements::HiddenField, 'input', :type => 'hidden') do |how, what|226 @browser.find_element(how, what).attribute('value')227 end228 end229 #230 # platform method to retrieve a hidden field element231 # See PageObject::Accessors#hidden_field232 #233 def hidden_field_for(identifier)234 find_selenium_element(identifier, Elements::HiddenField, 'input', :type => 'hidden')235 end236 #237 # platform method to retrieve all hidden field elements238 #239 def hidden_fields_for(identifier)240 find_selenium_elements(identifier, Elements::HiddenField, 'input', :type => 'hidden')241 end242 #243 # platform method to set text in a textarea244 # See PageObject::Accessors#text_area245 #246 def text_area_value_set(identifier, value)247 process_selenium_call(identifier, Elements::TextArea, 'textarea') do |how, what|248 text_area = @browser.find_element(how, what)249 text_area.clear250 text_area.send_keys(value)251 end252 end253 #254 # platform method to get the text from a textarea255 # See PageObject::Accessors#text_area256 #257 def text_area_value_for(identifier)258 process_selenium_call(identifier, Elements::TextArea, 'textarea') do |how, what|259 @browser.find_element(how, what).attribute('value')260 end261 end262 #263 # platform method to get the text area element264 # See PageObject::Accessors#text_area265 #266 def text_area_for(identifier)267 find_selenium_element(identifier, Elements::TextArea, 'textarea')268 end269 #270 # platform method to retrieve all text area elements271 #272 def text_areas_for(identifier)273 find_selenium_elements(identifier, Elements::TextArea, 'textarea')274 end275 #276 # platform method to get the currently selected value from a select list277 # See PageObject::Accessors#select_list278 #279 def select_list_value_for(identifier)280 process_selenium_call(identifier, Elements::SelectList, 'select') do |how, what|281 selected = nil282 @browser.find_element(how, what).find_elements(:tag_name => 'option').each do |o|283 if selected.nil?284 selected = o.text if o.selected?285 end286 end287 selected288 end289 end290 #291 # platform method to select a value from a select list292 # See PageObject::Accessors#select_list293 #294 def select_list_value_set(identifier, value)295 process_selenium_call(identifier, Elements::SelectList, 'select') do |how, what|296 select_list = @browser.find_element(how, what)297 select_list.find_elements(:tag_name => 'option').find do |option|298 option.text == value299 end.click300 end301 end302 #303 # platform method to return the select list element304 # See PageObject::Accessors#select_list305 #306 def select_list_for(identifier)307 find_selenium_element(identifier, Elements::SelectList, 'select')308 end309 #310 # platform method to retrieve all select list elements311 #312 def select_lists_for(identifier)313 find_selenium_elements(identifier, Elements::SelectList, 'select')314 end315 #316 # platform method to click a link317 # See PageObject::Accessors#link318 #319 def click_link_for(identifier)320 process_selenium_call(identifier, Elements::Link, 'a') do |how, what|321 @browser.find_element(how, what).click322 end323 end324 #325 # platform method to return a PageObject::Elements::Link object326 # see PageObject::Accessors#link327 #328 def link_for(identifier)329 find_selenium_element(identifier, Elements::Link, 'a')330 end331 #332 # platform method to retrieve all link elements333 #334 def links_for(identifier)335 find_selenium_elements(identifier, Elements::Link, 'a')336 end337 #338 # platform method to check a checkbox339 # See PageObject::Accessors#checkbox340 #341 def check_checkbox(identifier)342 process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|343 @browser.find_element(how, what).click unless @browser.find_element(how, what).selected?344 end345 end346 #347 # platform method to uncheck a checkbox348 # See PageObject::Accessors#checkbox349 #350 def uncheck_checkbox(identifier)351 process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|352 @browser.find_element(how, what).click if @browser.find_element(how, what).selected?353 end354 end355 #356 # platform method to determine if a checkbox is checked357 # See PageObject::Accessors#checkbox358 #359 def checkbox_checked?(identifier)360 process_selenium_call(identifier, Elements::CheckBox, 'input', :type => 'checkbox') do |how, what|361 @browser.find_element(how, what).selected?362 end363 end364 #365 # platform method to return a PageObject::Elements::CheckBox element366 # See PageObject::Accessors#checkbox367 #368 def checkbox_for(identifier)369 find_selenium_element(identifier, Elements::CheckBox, 'input', :type => 'checkbox')370 end371 #372 # platform method to retrieve all checkbox elements373 #374 def checkboxs_for(identifier)375 find_selenium_elements(identifier, Elements::CheckBox, 'input', :type => 'checkbox')376 end377 #378 # platform method to select a radio button379 # See PageObject::Accessors#radio_button380 #381 def select_radio(identifier)382 process_selenium_call(identifier, Elements::RadioButton, 'input', :type => 'radio') do |how, what|383 @browser.find_element(how, what).click unless @browser.find_element(how, what).selected?384 end385 end386 #387 # platform method to clear a radio button388 # See PageObject::Accessors#radio_button389 #390 def clear_radio(identifier)391 process_selenium_call(identifier, Elements::RadioButton, 'input', :type => 'radio') do |how, what|392 @browser.find_element(how, what).click if @browser.find_element(how, what).selected?393 end394 end395 #396 # platform method to determine if a radio button is selected397 # See PageObject::Accessors#radio_button398 #399 def radio_selected?(identifier)400 process_selenium_call(identifier, Elements::RadioButton, 'input', :type => 'radio') do |how, what|401 @browser.find_element(how, what).selected?402 end403 end404 #405 # platform method to return a PageObject::Eements::RadioButton element406 # See PageObject::Accessors#radio_button407 #408 def radio_button_for(identifier)409 find_selenium_element(identifier, Elements::RadioButton, 'input', :type => 'radio')410 end411 #412 # platform method to retrieve all radio button elements413 #414 def radio_buttons_for(identifier)415 find_selenium_elements(identifier, Elements::RadioButton, 'input', :type => 'radio')416 end417 #418 # platform method to return the text for a div419 # See PageObject::Accessors#div420 #421 def div_text_for(identifier)422 process_selenium_call(identifier, Elements::Div, 'div') do |how, what|423 @browser.find_element(how, what).text424 end425 end426 #427 # platform method to return a PageObject::Elements::Div element428 # See PageObject::Accessors#div429 #430 def div_for(identifier)431 find_selenium_element(identifier, Elements::Div, 'div')432 end433 #434 # platform method to retrieve all div elements435 #436 def divs_for(identifier)437 find_selenium_elements(identifier, Elements::Div, 'div')438 end439 #440 # platform method to return the text for a span441 # See PageObject::Accessors#span442 #443 def span_text_for(identifier)444 process_selenium_call(identifier, Elements::Span, 'span') do |how, what|445 @browser.find_element(how, what).text446 end447 end448 #449 # platform method to return a PageObject::Elements::Span element450 # See PageObject::Accessors#span451 #452 def span_for(identifier)453 find_selenium_element(identifier, Elements::Span, 'span')454 end455 #456 # platform method to retrieve all span elements457 #458 def spans_for(identifier)459 find_selenium_elements(identifier, Elements::Span, 'span')460 end461 #462 # platform method to click a button463 # See PageObject::Accessors#button464 #465 def click_button_for(identifier)466 process_selenium_call(identifier, Elements::Button, 'input', :type => 'submit') do |how, what|467 @browser.find_element(how, what).click468 end469 end470 #471 # platform method to retrieve a button element472 # See PageObject::Accessors#button473 #474 def button_for(identifier)475 find_selenium_element(identifier, Elements::Button, 'input', :type => 'submit')476 end477 #478 # platform method to retrieve an array of button elements479 #480 def buttons_for(identifier)481 find_selenium_elements(identifier, Elements::Button, 'input', :type => 'submit')482 end483 484 #485 # platform method to return the text for a table486 # See PageObject::Accessors#table487 #488 def table_text_for(identifier)489 process_selenium_call(identifier, Elements::Table, 'table') do |how, what|490 @browser.find_element(how, what).text491 end492 end493 #494 # platform method to retrieve a table element495 # See PageObject::Accessors#table496 #497 def table_for(identifier)498 find_selenium_element(identifier, Elements::Table, 'table')499 end500 #501 # platform method to retrieve all table elements502 #503 def tables_for(identifier)504 find_selenium_elements(identifier, Elements::Table, 'table')505 end506 #507 # platform method to retrieve the text from a table cell508 # See PageObject::Accessors#cell509 #510 def cell_text_for(identifier)511 process_selenium_call(identifier, Elements::TableCell, 'td') do |how, what|512 @browser.find_element(how, what).text513 end514 end515 #516 # platform method to retrieve a table cell element517 # See PageObject::Accessors#cell518 #519 def cell_for(identifier)520 find_selenium_element(identifier, Elements::TableCell, 'td')521 end522 #523 # platform method to retrieve all table cell elements524 #525 def cells_for(identifier)526 find_selenium_elements(identifier, Elements::TableCell, 'td')527 end528 #529 # platform method to retrieve an image element530 # See PageObject::Accessors#image531 #532 def image_for(identifier)533 find_selenium_element(identifier, Elements::Image, 'img')534 end535 #536 # platform method to retrieve all image elements537 #538 def images_for(identifier)539 find_selenium_elements(identifier, Elements::Image, 'img')540 end541 #542 # platform method to retrieve a form element543 # See PageObject::Accessors#form544 #545 def form_for(identifier)546 find_selenium_element(identifier, Elements::Form, 'form')547 end548 #549 # platform method to retrieve all forms550 #551 def forms_for(identifier)552 find_selenium_elements(identifier, Elements::Form, 'form')553 end554 #555 # platform method to retrieve the text from a list item556 # See PageObject::Accessors#list_item557 #558 def list_item_text_for(identifier)559 process_selenium_call(identifier, Elements::ListItem, 'li') do |how, what|560 @browser.find_element(how, what).text561 end562 end563 #564 # platform method to retrieve a list item element565 # See PageObject::Accessors#list_item566 #567 def list_item_for(identifier)568 find_selenium_element(identifier, Elements::ListItem, 'li')569 end570 #571 # platform method to retrieve all list items572 #573 def list_items_for(identifier)574 find_selenium_elements(identifier, Elements::ListItem, 'li')575 end576 #577 # platform method to retrieve the text from an unordered list578 # See PageObject::Accessors#unordered_list579 #580 def unordered_list_text_for(identifier)581 process_selenium_call(identifier, Elements::UnorderedList, 'ul') do |how, what|582 @browser.find_element(how, what).text583 end584 end585 #586 # platform method to retrieve an unordered list element587 # See PageObject::Accessors#unordered_list588 #589 def unordered_list_for(identifier)590 find_selenium_element(identifier, Elements::UnorderedList, 'ul')591 end592 #593 # platform method to retrieve all unordered lists594 #595 def unordered_lists_for(identifier)596 find_selenium_elements(identifier, Elements::UnorderedList, 'ul')597 end598 #599 # platform method to retrieve the text from an ordered list600 # See PageObject::Accessors#ordered_list601 #602 def ordered_list_text_for(identifier)603 process_selenium_call(identifier, Elements::OrderedList, 'ol') do |how, what|604 @browser.find_element(how, what).text605 end606 end607 #608 # platform method to retrieve an ordered list element609 # See PageObject::Accessors#ordered_list610 #611 def ordered_list_for(identifier)612 find_selenium_element(identifier, Elements::OrderedList, 'ol')613 end614 615 #616 # platform method to retrieve all ordered lists617 #618 def ordered_lists_for(identifier)619 find_selenium_elements(identifier, Elements::OrderedList, 'ol')620 end621 #622 # platform method to retrieve the text from a h1623 # See PageObject::Accessors#h1624 #625 def h1_text_for(identifier)626 process_selenium_call(identifier, Elements::Heading, 'h1') do |how, what|627 @browser.find_element(how, what).text628 end629 end630 631 #632 # platform method to retrieve the h1 element633 # See PageObject::Accessors#h1634 #635 def h1_for(identifier)636 find_selenium_element(identifier, Elements::Heading, 'h1')637 end638 #639 # platform method to retrieve all h1 elements640 #641 def h1s_for(identifier)642 find_selenium_elements(identifier, Elements::Heading, 'h1')643 end644 #645 # platform method to retrieve the text from a h2646 # See PageObject::Accessors#h2647 #648 def h2_text_for(identifier)649 process_selenium_call(identifier, Elements::Heading, 'h2') do |how, what|650 @browser.find_element(how, what).text651 end652 end653 654 #655 # platform method to retrieve the h2 element656 # See PageObject::Accessors#h2657 #658 def h2_for(identifier)659 find_selenium_element(identifier, Elements::Heading, 'h2')660 end661 #662 # platform method to retrieve all h2 elements663 #664 def h2s_for(identifier)665 find_selenium_elements(identifier, Elements::Heading, 'h2')666 end667 #668 # platform method to retrieve the text from a h3669 # See PageObject::Accessors#h3670 #671 def h3_text_for(identifier)672 process_selenium_call(identifier, Elements::Heading, 'h3') do |how, what|673 @browser.find_element(how, what).text674 end675 end676 677 #678 # platform method to retrieve the h3 element679 # See PageObject::Accessors#h3680 #681 def h3_for(identifier)682 find_selenium_element(identifier, Elements::Heading, 'h3')683 end684 #685 # platform method to retrieve all h3 elements686 #687 def h3s_for(identifier)688 find_selenium_elements(identifier, Elements::Heading, 'h3')689 end690 #691 # platform method to retrieve the text from a h4692 # See PageObject::Accessors#h4693 #694 def h4_text_for(identifier)695 process_selenium_call(identifier, Elements::Heading, 'h4') do |how, what|696 @browser.find_element(how, what).text697 end698 end699 700 #701 # platform method to retrieve the h4 element702 # See PageObject::Accessors#h4703 #704 def h4_for(identifier)705 find_selenium_element(identifier, Elements::Heading, 'h4')706 end707 #708 # platform method to retrieve all h4 elements709 #710 def h4s_for(identifier)711 find_selenium_elements(identifier, Elements::Heading, 'h4')712 end713 #714 # platform method to retrieve the text from a h5715 # See PageObject::Accessors#h5716 #717 def h5_text_for(identifier)718 process_selenium_call(identifier, Elements::Heading, 'h5') do |how, what|719 @browser.find_element(how, what).text720 end721 end722 723 #724 # platform method to retrieve the h5 element725 # See PageObject::Accessors#h5726 #727 def h5_for(identifier)728 find_selenium_element(identifier, Elements::Heading, 'h5')729 end730 #731 # platform method to retrieve all h5 elements732 #733 def h5s_for(identifier)734 find_selenium_elements(identifier, Elements::Heading, 'h5')735 end736 #737 # platform method to retrieve the text from a h6738 # See PageObject::Accessors#h6739 #740 def h6_text_for(identifier)741 process_selenium_call(identifier, Elements::Heading, 'h6') do |how, what|742 @browser.find_element(how, what).text743 end744 end745 746 #747 # platform method to retrieve the h6 element748 # See PageObject::Accessors#h6749 #750 def h6_for(identifier)751 find_selenium_element(identifier, Elements::Heading, 'h6')752 end753 #754 # platform method to retrieve all h6 elements755 #756 def h6s_for(identifier)757 find_selenium_elements(identifier, Elements::Heading, 'h6')758 end759 #760 # platform method to retrieve the text for a paragraph761 # See PageObject::Accessors#paragraph762 #763 def paragraph_text_for(identifier)764 process_selenium_call(identifier, Elements::Paragraph, 'p') do |how, what|765 @browser.find_element(how, what).text766 end767 end768 769 #770 # platform method to retrieve the paragraph element771 # See PageObject::Accessors#paragraph772 #773 def paragraph_for(identifier)774 find_selenium_element(identifier, Elements::Paragraph, 'p')775 end776 #777 # platform method to retrieve all paragraph elements778 #779 def paragraphs_for(identifier)780 find_selenium_elements(identifier, Elements::Paragraph, 'p')781 end782 #783 # platform method to return the text for a label784 # See PageObject::Accessors#label785 #786 def label_text_for(identifier)787 process_selenium_call(identifier, Elements::Label, 'label') do |how, what|788 @browser.find_element(how, what).text789 end790 end791 #792 # platform method to return a PageObject::Elements::Label element793 # See PageObject::Accessors#label794 #795 def label_for(identifier)796 find_selenium_element(identifier, Elements::Label, 'label')797 end798 #799 # platform method to retrieve all label elements800 #801 def labels_for(identifier)802 find_selenium_elements(identifier, Elements::Label, 'label')803 end804 #805 # platform method to set the file on a file_field element806 # See PageObject::Accessors#file_field807 #808 def file_field_value_set(identifier, value)809 process_selenium_call(identifier, Elements::FileField, 'input', :type => 'file') do |how, what|810 @browser.find_element(how, what).send_keys(value)811 end812 end813 #814 # platform method to retrieve a file_field element815 # See PageObject::Accessors#file_field816 #817 def file_field_for(identifier)818 find_selenium_element(identifier, Elements::FileField, 'input', :type => 'file')819 end820 #821 # platform method to return an array of file field elements822 #823 def file_fields_for(identifier)824 find_selenium_elements(identifier, Elements::FileField, 'input', :type => 'file')825 end826 #827 # platform method to click on an area828 #829 def click_area_for(identifier)830 process_selenium_call(identifier, Elements::Area, 'area') do |how, what|831 @browser.find_element(how, what).click832 end833 end834 #835 # platform method to retrieve an area element836 #837 def area_for(identifier)838 find_selenium_element(identifier, Elements::Area, 'area')839 end840 #841 # platform method to return an array of area elements842 #843 def areas_for(identifier)844 find_selenium_elements(identifier, Elements::Area, 'area')845 end846 #847 # platform method to retrieve a canvas element848 #849 def canvas_for(identifier)850 find_selenium_element(identifier, Elements::Canvas, 'canvas')851 end852 #853 # platform method to return an array of canvas elements854 #855 def canvass_for(identifier)856 find_selenium_elements(identifier, Elements::Canvas, 'canvas')857 end858 859 #860 # platform method to retrieve an audio element861 #862 def audio_for(identifier)863 find_selenium_element(identifier, Elements::Audio, 'audio')864 end865 #866 # platform method to return an array of audio elements867 #868 def audios_for(identifier)869 find_selenium_elements(identifier, Elements::Audio, 'audio')870 end871 872 #873 # platform method to retrieve a video element874 #875 def video_for(identifier)876 find_selenium_element(identifier, Elements::Video, 'video')877 end878 #879 # platform method to return an array of video elements880 #881 def videos_for(identifier)882 find_selenium_elements(identifier, Elements::Video, 'video')883 end884 885 #886 # platform method to retrieve a generic element887 # See PageObject::Accessors#element888 #889 def element_for(tag, identifier)890 find_selenium_element(identifier, Elements::Element, tag.to_s)891 end892 #893 # platform method to retrieve a collection of generic elements894 # See PageObject::Accessors#elements895 #896 def elements_for(tag, identifier)897 find_selenium_elements(identifier, Elements::Element, tag.to_s)898 end899 #900 # platform method to return a svg element901 #902 def svg_for(identifier)903 find_selenium_element(identifier, Elements::Element, 'svg')904 end905 906 #907 # platform method to return an array of svg elements908 #909 def svgs_for(identifier)910 find_selenium_elements(identifier, Elements::Element, 'svg')911 end912 913 private914 915 def process_selenium_call(identifier, type, tag, other=nil)916 how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)917 switch_to_frame(frame_identifiers)918 value = yield how, what919 @browser.switch_to.default_content unless frame_identifiers.nil?920 value921 end922 def find_selenium_element(identifier, type, tag, other=nil)923 how, what, frame_identifiers = parse_identifiers(identifier, type, tag, other)924 switch_to_frame(frame_identifiers)925 begin926 element = @browser.find_element(how, what)927 rescue Selenium::WebDriver::Error::NoSuchElementError928 @browser.switch_to.default_content unless frame_identifiers.nil?929 return build_null_object(identifier, type, tag, other)...
capabilities.rb
Source:capabilities.rb
...15 @selenium_browser = options[:url] ? :remote : @browser16 @selenium_opts = {}17 end18 def to_args19 [@selenium_browser, process_arguments]20 end21 private22 def process_arguments23 @selenium_opts[:listener] = @options.delete(:listener) if @options.key?(:listener)24 if @options.key?(:url)25 @selenium_opts[:url] = @options.delete(:url)26 else27 process_service(@options.delete(:service))28 end29 process_http_client30 process_browser_options31 process_capabilities32 Watir.logger.info "Creating Browser instance with Watir processed options: #{@selenium_opts.inspect}"33 @selenium_opts34 end35 def process_http_client36 http_client = @options.delete(:http_client) || Watir::HttpClient.new37 if http_client.is_a?(Hash)38 http_client = Watir::HttpClient.new(http_client)39 elsif !http_client.is_a?(Selenium::WebDriver::Remote::Http::Common)40 raise TypeError, ':http_client must be a Hash or a Selenium HTTP Client instance'41 end42 unless http_client.is_a?(Watir::HttpClient)43 Watir.logger.warn 'Check out the new Watir::HttpClient and let us know if there are missing features you need',44 ids: [:watir_client]45 end46 process_http_client_timeouts(http_client)47 @selenium_opts[:http_client] = http_client48 end49 def process_browser_options50 browser_options = @options.delete(:options) || {}51 process_w3c_capabilities(browser_options)52 case @browser53 when :chrome54 process_chrome_options(browser_options)55 when :firefox56 process_firefox_options(browser_options)57 when :safari58 process_safari_options(browser_options)59 when :ie, :internet_explorer60 process_ie_options(browser_options)61 end62 end63 def process_capabilities64 caps = @options.delete(:capabilities)65 unless @options.empty?66 Watir.logger.deprecate('passing unrecognized arguments into Browser constructor',67 'appropriate keyword to nest all arguments',68 ids: %i[unknown_keyword capabilities],69 reference: 'http://watir.com/guides/capabilities.html')70 end71 if caps72 @selenium_opts.merge!(@options)73 else74 caps = Selenium::WebDriver::Remote::Capabilities.send @browser, @options.merge(@w3c_caps)75 end76 @selenium_opts[:desired_capabilities] = caps77 end78 def deprecate_desired_capabilities79 return unless @options.key?(:desired_capabilities)80 Watir.logger.deprecate(':desired_capabilities to initialize Browser',81 ':capabilities or preferably :options',82 ids: [:desired_capabilities],83 reference: 'http://watir.com/guides/capabilities.html')84 @options[:capabilities] = @options.delete(:desired_capabilities)85 end86 def deprecate_url_service87 Watir.logger.deprecate('allowing Browser initialization with both :url & :service',88 'just :service',89 ids: [:url_service],90 reference: 'http://watir.com/guides/capabilities.html')91 end92 def process_http_client_timeouts(http_client)93 deprecate_client_timeout(http_client) if @options.key? :client_timeout94 deprecate_open_timeout(http_client) if @options.key? :open_timeout95 deprecate_read_timeout(http_client) if @options.key? :read_timeout96 end97 def deprecate_client_timeout(http_client)98 Watir.logger.deprecate(':client_timeout to initialize Browser',99 ':open_timeout and/or :read_timeout in a Hash with :http_client key',100 ids: [:http_client_timeout],101 reference: 'http://watir.com/guides/capabilities.html')102 timeout = @options.delete(:client_timeout)103 http_client.open_timeout = timeout104 http_client.read_timeout = timeout105 end106 def deprecate_open_timeout(http_client)107 Watir.logger.deprecate(':open_timeout to initialize Browser',108 ':open_timeout in a Hash with :http_client key',109 ids: %i[http_open_timeout capabilities],110 reference: 'http://watir.com/guides/capabilities.html')111 http_client.open_timeout = @options.delete(:open_timeout)112 end113 def deprecate_read_timeout(http_client)114 Watir.logger.deprecate(':read_timeout to initialize Browser',115 ':read_timeout in a Hash with :http_client key',116 ids: %i[http_read_timeout capabilities],117 reference: 'http://watir.com/guides/capabilities.html')118 http_client.read_timeout = @options.delete(:read_timeout)119 end120 def process_chrome_options(browser_options)121 @selenium_opts[:options] = browser_options if browser_options.is_a? Selenium::WebDriver::Chrome::Options122 @selenium_opts[:options] ||= Selenium::WebDriver::Chrome::Options.new(**browser_options)123 process_args124 return unless @options.delete(:headless)125 @selenium_opts[:options].args << '--headless'126 @selenium_opts[:options].args << '--disable-gpu'127 end128 def process_firefox_options(browser_options)129 @selenium_opts[:options] = browser_options if browser_options.is_a? Selenium::WebDriver::Firefox::Options130 @selenium_opts[:options] ||= Selenium::WebDriver::Firefox::Options.new(**browser_options)131 if @options.key?(:profile)132 new = 'Initializing Browser with both :profile and :option'133 old = ':profile as a key inside :option'134 Watir.logger.deprecate new, old, ids: [:firefox_profile]135 @selenium_opts[:options].profile = @options.delete(:profile)136 end137 @selenium_opts[:options].args << '--headless' if @options.delete(:headless)138 end139 def process_safari_options(browser_options)140 @selenium_opts[:options] = browser_options if browser_options.is_a? Selenium::WebDriver::Safari::Options141 @selenium_opts[:options] ||= Selenium::WebDriver::Safari::Options.new(**browser_options)142 Selenium::WebDriver::Safari.technology_preview! if @options.delete(:technology_preview)143 end144 def process_ie_options(browser_options)145 @selenium_opts[:options] = browser_options if browser_options.is_a? Selenium::WebDriver::IE::Options146 @selenium_opts[:options] ||= Selenium::WebDriver::IE::Options.new(**browser_options)147 process_args148 end149 def process_service(service)150 service = deprecate_service_keywords if service.nil?151 @selenium_opts[:service] = case service152 when Hash153 return if service.empty?154 Selenium::WebDriver::Service.send(@browser, service)155 when Selenium::WebDriver::Service156 service157 else158 raise TypeError, "#{service} needs to be Selenium Service or Hash instance"159 end160 end161 def deprecate_service_keywords162 service = {}163 if @options.key?(:port)164 Watir.logger.deprecate(':port to initialize Browser',165 ':port in a Hash with :service key',166 ids: %i[port_keyword capabilities],167 reference: 'http://watir.com/guides/capabilities.html')168 service[:port] = @options.delete(:port)169 end170 if @options.key?(:driver_opts)171 Watir.logger.deprecate(':driver_opts to initialize Browser',172 ':args as Array in a Hash with :service key',173 ids: %i[driver_opts_keyword capabilities],174 reference: 'http://watir.com/guides/capabilities.html')175 service[:args] = @options.delete(:driver_opts)176 end177 service178 end179 def process_args180 args = if @options.key?(:args)181 deprecate_args182 @options.delete(:args)183 elsif @options.key?(:switches)184 deprecate_switches185 @options.delete(:switches)186 else187 []188 end189 args.each { |arg| @selenium_opts[:options].args << arg }190 end191 def deprecate_args192 Watir.logger.deprecate(':args to initialize Browser',193 ':args inside Hash with :options key',194 ids: %i[args_keyword capabilities],195 reference: 'http://watir.com/guides/capabilities.html')196 end197 def deprecate_switches198 Watir.logger.deprecate(':switches to initialize Browser',199 ':switches inside Hash with :options key',200 ids: %i[switches_keyword capabilities],201 reference: 'http://watir.com/guides/capabilities.html')202 end203 def deprecate_remote(browser)204 return unless browser == :remote205 Watir.logger.deprecate(':remote to initialize Browser',206 'browser key along with remote url',207 ids: %i[remote_keyword capabilities],208 reference: 'http://watir.com/guides/capabilities.html')209 infer_browser210 end211 def infer_browser212 if @options.key?(:browser)213 @options.delete(:browser)214 elsif @options.key?(:capabilities)215 @options[:capabilities].browser_name.tr(' ', '_').to_sym216 elsif @options.key?(:options)217 @options[:options].class.to_s.split('::')[-2].downcase.to_sym218 else219 :chrome220 end221 end222 def process_w3c_capabilities(opts)223 @w3c_caps = {}224 return unless opts.is_a?(Hash)225 w3c_keys = %i[browser_version platform_name accept_insecure_certs page_load_strategy proxy set_window_rect226 timeouts unhandled_prompt_behavior strict_file_interactibility]227 opts.each do |key, _val|228 next unless key.to_s.include?(':') || w3c_keys.include?(key)229 @w3c_caps[key] = opts.delete(key)230 end231 end232 def deprecate_options_capabilities233 return unless @options.key?(:capabilities) && @options.key?(:options)234 old = 'initializing Browser with both options and capabilities'235 new = 'Hash with :options, Selenium Options instance with :options or' \236'Selenium Capabilities instance with :capabilities'...
server.rb
Source:server.rb
...15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY16# KIND, either express or implied. See the License for the17# specific language governing permissions and limitations18# under the License.19require 'childprocess'20require 'selenium/webdriver/common/socket_poller'21require 'net/http'22module Selenium23 #24 # Wraps the remote server jar25 #26 # Usage:27 #28 # server = Selenium::Server.new('/path/to/selenium-server-standalone.jar')29 # server.start30 #31 # Automatically download the given version:32 #33 # server = Selenium::Server.get '2.6.0'34 # server.start35 #36 # or the latest version:37 #38 # server = Selenium::Server.get :latest39 # server.start40 #41 # Run the server in the background:42 #43 # server = Selenium::Server.new(jar, :background => true)44 # server.start45 #46 # Add additional arguments:47 #48 # server = Selenium::Server.new(jar)49 # server << ["--additional", "args"]50 # server.start51 #52 class Server53 class Error < StandardError; end54 CL_RESET = WebDriver::Platform.windows? ? '' : "\r\e[0K"55 def self.get(required_version, opts = {})56 new(download(required_version), opts)57 end58 #59 # Download the given version of the selenium-server-standalone jar.60 #61 class << self62 def download(required_version)63 required_version = latest if required_version == :latest64 download_file_name = "selenium-server-standalone-#{required_version}.jar"65 return download_file_name if File.exist? download_file_name66 begin67 open(download_file_name, 'wb') do |destination|68 net_http.start('selenium-release.storage.googleapis.com') do |http|69 resp = http.request_get("/#{required_version[/(\d+\.\d+)\./, 1]}/#{download_file_name}") do |response|70 total = response.content_length71 progress = 072 segment_count = 073 response.read_body do |segment|74 progress += segment.length75 segment_count += 176 if segment_count % 15 == 077 percent = (progress.to_f / total.to_f) * 10078 print "#{CL_RESET}Downloading #{download_file_name}: #{percent.to_i}% (#{progress} / #{total})"79 segment_count = 080 end81 destination.write(segment)82 end83 end84 unless resp.is_a? Net::HTTPSuccess85 raise Error, "#{resp.code} for #{download_file_name}"86 end87 end88 end89 rescue90 FileUtils.rm download_file_name if File.exist? download_file_name91 raise92 end93 download_file_name94 end95 #96 # Ask Google Code what the latest selenium-server-standalone version is.97 #98 def latest99 require 'rexml/document'100 net_http.start('selenium-release.storage.googleapis.com') do |http|101 REXML::Document.new(http.get('/').body).root.get_elements('//Contents/Key').map do |e|102 e.text[/selenium-server-standalone-(\d+\.\d+\.\d+)\.jar/, 1]103 end.compact.max104 end105 end106 def net_http107 http_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']108 if http_proxy109 http_proxy = "http://#{http_proxy}" unless http_proxy.start_with?('http://')110 uri = URI.parse(http_proxy)111 Net::HTTP::Proxy(uri.host, uri.port)112 else113 Net::HTTP114 end115 end116 end117 #118 # The server port119 #120 attr_accessor :port121 #122 # The server timeout123 #124 attr_accessor :timeout125 #126 # Whether to launch the server in the background127 #128 attr_accessor :background129 #130 # Path to log file, or 'true' for stdout.131 #132 attr_accessor :log133 #134 # @param [String] jar Path to the server jar.135 # @param [Hash] opts the options to create the server process with136 #137 # @option opts [Integer] :port Port the server should listen on (default: 4444).138 # @option opts [Integer] :timeout Seconds to wait for server launch/shutdown (default: 30)139 # @option opts [true,false] :background Run the server in the background (default: false)140 # @option opts [true,false,String] :log Either a path to a log file,141 # or true to pass server log to stdout.142 # @raise [Errno::ENOENT] if the jar file does not exist143 #144 def initialize(jar, opts = {})145 raise Errno::ENOENT, jar unless File.exist?(jar)146 @jar = jar147 @host = '127.0.0.1'148 @port = opts.fetch(:port, 4444)149 @timeout = opts.fetch(:timeout, 30)150 @background = opts.fetch(:background, false)151 @log = opts[:log]152 @additional_args = []153 end154 def start155 process.start156 poll_for_service157 process.wait unless @background158 end159 def stop160 begin161 Net::HTTP.get(@host, '/selenium-server/driver/?cmd=shutDownSeleniumServer', @port)162 rescue Errno::ECONNREFUSED163 end164 stop_process if @process165 poll_for_shutdown166 @log_file.close if @log_file167 end168 def webdriver_url169 "http://#{@host}:#{@port}/wd/hub"170 end171 def <<(arg)172 if arg.is_a?(Array)173 @additional_args += arg174 else175 @additional_args << arg.to_s176 end177 end178 private179 def stop_process180 return unless @process.alive?181 begin182 @process.poll_for_exit(5)183 rescue ChildProcess::TimeoutError184 @process.stop185 end186 rescue Errno::ECHILD187 # already dead188 ensure189 @process = nil190 end191 def process192 @process ||= (193 # extract any additional_args that start with -D as options194 properties = @additional_args.dup - @additional_args.delete_if { |arg| arg[/^-D/] }195 server_command = ['java'] + properties + ['-jar', @jar, '-port', @port.to_s] + @additional_args196 cp = ChildProcess.build(*server_command)197 WebDriver.logger.debug("Executing Process #{server_command}")198 io = cp.io199 if @log.is_a?(String)200 @log_file = File.open(@log, 'w')201 io.stdout = io.stderr = @log_file202 elsif @log203 io.inherit!204 end205 cp.detach = @background206 cp...
server_spec.rb
Source:server_spec.rb
1require File.expand_path('../webdriver/spec_helper', __FILE__)2require 'selenium/server'3describe Selenium::Server do4 let(:mock_process) { double(ChildProcess).as_null_object }5 let(:mock_poller) { double("SocketPoller", :connected? => true, :closed? => true)}6 it "raises an error if the jar file does not exist" do7 lambda {8 Selenium::Server.new("doesnt-exist.jar")9 }.should raise_error(Errno::ENOENT)10 end11 it "uses the given jar file and port" do12 File.should_receive(:exist?).with("selenium-server-test.jar").and_return(true)13 ChildProcess.should_receive(:build).14 with("java", "-jar", "selenium-server-test.jar", "-port", "1234").15 and_return(mock_process)16 server = Selenium::Server.new("selenium-server-test.jar", :port => 1234, :background => true)17 server.stub(:socket).and_return(mock_poller)18 server.start19 end20 it "waits for the server process by default" do21 File.should_receive(:exist?).with("selenium-server-test.jar").and_return(true)22 ChildProcess.should_receive(:build).23 with("java", "-jar", "selenium-server-test.jar", "-port", "4444").24 and_return(mock_process)25 server = Selenium::Server.new("selenium-server-test.jar")26 server.stub(:socket).and_return(mock_poller)27 mock_process.should_receive(:wait)28 server.start29 end30 it "adds additional args" do31 File.should_receive(:exist?).with("selenium-server-test.jar").and_return(true)32 ChildProcess.should_receive(:build).33 with("java", "-jar", "selenium-server-test.jar", "-port", "4444", "foo", "bar").34 and_return(mock_process)35 server = Selenium::Server.new("selenium-server-test.jar", :background => true)36 server.stub(:socket).and_return(mock_poller)37 server << ["foo", "bar"]38 server.start39 end40 it "downloads the specified version from the selenium site" do41 required_version = '10.2.0'42 expected_download_file_name = "selenium-server-standalone-#{required_version}.jar"43 stub_request(:get, "http://selenium-release.storage.googleapis.com/10.2/selenium-server-standalone-10.2.0.jar").to_return(:body => "this is pretending to be a jar file for testing purposes")44 begin45 actual_download_file_name = Selenium::Server.download(required_version)46 actual_download_file_name.should == expected_download_file_name47 File.should exist(expected_download_file_name)48 ensure...
selenium_webdriver_phantomjs_monkey_patch.rb
Source:selenium_webdriver_phantomjs_monkey_patch.rb
...16 true17 end18end19##20# Don't start new PhantomJS process21#22Selenium::WebDriver::PhantomJS::Service.class_eval do23 def start(args = [])24 require 'selenium/webdriver/common'25 if @process && @process.alive?26 raise "already started: #{@uri.inspect} #{@executable.inspect}"27 end28 puts "Starting monkey-patched PhantomJS Selenium Webdriver"29 # @process = create_process(args)30 # @process.start31 socket_poller = Selenium::WebDriver::SocketPoller.new Selenium::WebDriver::Platform.localhost, @uri.port, Selenium::WebDriver::PhantomJS::Service::START_TIMEOUT32 unless socket_poller.connected?33 raise Selenium::WebDriver::Error::WebDriverError, "unable to connect to phantomjs @ #{@uri} after #{Selenium::WebDriver::PhantomJS::Service::START_TIMEOUT} seconds"34 end35 Selenium::WebDriver::Platform.exit_hook { stop } # make sure we don't leave the server running36 end37end...
process
Using AI Code Generation
1 selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000)2 selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000)3 selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000)4 selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000)
process
Using AI Code Generation
1 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)2 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)3 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)4 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)
process
Using AI Code Generation
1selenium.find_element(:name, "q").send_keys "Hello World"2selenium.find_element(:name, "btnG").click3driver.find_element(:name, "q").send_keys "Hello World"4driver.find_element(:name, "btnG").click5driver.find_element(:name, "q").send_keys "Hello World"6driver.find_element(:name, "btnG").click7driver.find_element(:name, "q").send_keys "Hello World"8driver.find_element(:name, "btnG").click9driver.find_element(:name, "q").send_keys "Hello World"10driver.find_element(:name, "btnG").click11driver.find_element(:name, "q").send_keys "Hello World"12driver.find_element(:name, "btnG").click13driver.find_element(:name, "q").send_keys "Hello World"14driver.find_element(:name, "btnG").click
process
Using AI Code Generation
1 @driver.get(@base_url + "/")2 @driver.find_element(:name, "q").clear3 @driver.find_element(:name, "q").send_keys "selenium"4 @driver.find_element(:name, "btnG").click5 @driver.find_element(:link, "Selenium - Web Browser Automation").click6 @driver.get(@base_url + "/")7 @driver.find_element(:name, "q").clear8 @driver.find_element(:name, "q").send_keys "selenium"9 @driver.find_element(:name, "btnG").click10 @driver.find_element(:link, "Selenium - Web Browser Automation").click11 @driver.get(@base_url + "/")12 @driver.find_element(:name, "q").clear13 @driver.find_element(:name, "q").send_keys "selenium"14 @driver.find_element(:name, "btnG").click
process
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys 'Selenium'2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys 'Selenium'4driver.find_element(:name, 'btnG').click5driver.find_element(:name, 'q').send_keys 'Selenium'6driver.find_element(:name, 'btnG').click7driver.find_element(:name, 'q').send_keys 'Selenium'8driver.find_element(:name, 'btnG').click9 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000)
process
Using AI Code Generation
1 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)2 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)3 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)4 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)
process
Using AI Code Generation
1 selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000)2 selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000)3 selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000)4 selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000)
process
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys 'Selenium'2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys 'Selenium'4driver.find_element(:name, 'btnG').click5driver.find_element(:name, 'q').send_keys 'Selenium'6driver.find_element(:name, 'btnG').click7driver.find_element(:name, 'q').send_keys 'Selenium'8driver.find_element(:name, 'btnG').click9 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)10 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)11 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)12 sel = Selenium::SeleniumDriver.new("localhost", 4444, "*iexplore", "http://www.google.com", 10000)
process
Using AI Code Generation
1 @driver.get(@base_url + "/")2 @driver.find_element(:name, "q").clear3 @driver.find_element(:name, "q").send_keys "selenium"4 @driver.find_element(:name, "btnG").click5 @driver.find_element(:link, "Selenium - Web Browser Automation").click6 @driver.get(@base_url + "/")7 @driver.find_element(:name, "q").clear8 @driver.find_element(:name, "q").send_keys "selenium"9 @driver.find_element(:name, "btnG").click10 @driver.find_element(:link, "Selenium - Web Browser Automation").click11 @driver.get(@base_url + "/")12 @driver.find_element(:name, "q").clear13 @driver.find_element(:name, "q").send_keys "selenium"14 @driver.find_element(:name, "btnG").click
process
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys 'Selenium'2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys 'Selenium'4driver.find_element(:name, 'btnG').click5driver.find_element(:name, 'q').send_keys 'Selenium'6driver.find_element(:name, 'btnG').click7driver.find_element(:name, 'q').send_keys 'Selenium'8driver.find_element(:name, 'btnG').click
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!