Best Selenium code snippet using Selenium.WebDriver.css_value
web_element.rb
Source:web_element.rb
...230 element.js_click231 end232 self233 end234 def css_value(prop)235 element.css_value(prop)236 end237 def hover238 $driver.action.move_to(element).perform239 end240 def displayed?241 if not exists?242 return false243 end244 return @element.displayed?245 end246 def parent247 return element.find_element(:xpath,"..")248 end249 def present?250 exists?251 end252 def enabled?253 element.enabled?254 end255 def location256 element.location257 end258 def location_once_scrolled_into_view259 element.location_once_scrolled_into_view260 end261 def selectable?262 element.selectable?263 end264 def selected?265 element.selected?266 end267 def check(is_checked=true)268 sel = selected?269 if selected? != is_checked270 element.click271 end272 end273 def send_keys(*args)274 element.send_keys(args)275 end276 def size277 element.size278 end279 def submit280 element.submit281 end282 def tag_name283 element.tag_name284 end285 def text286 element.text287 end288 def value289 element.attribute("value")290 end291 def to_json(*args)292 element.to_json(args)293 end294 def find_elements(by, value, name=NIL)295 element.find_elements(by, value, name)296 end297 def find_element(by, value, name=NIL)298 element.find_element(by, value, name)299 end300 def highlight(time=0.1, color="yellow")301 element.highlight(time, color)302 end303 def set_text(value)304 element.set_text(value)305 end306 def is_stale?307 begin308 @element.enabled?309 return false310 rescue Exception => e311 return true312 end313 end314 def html()315 attribute("outerHTML")316 rescue Selenium::WebDriver::Error::StaleElementReferenceError317 end318 def scroll_into_view()319 $driver.execute_script("arguments[0].scrollIntoView(); return;", self)320 self321 end322 def js_click323 $logger.info "JS Clicking Element #{self}"324 $driver.execute_script("arguments[0].click(); return;", self)325 self.highlight(color="red")326 self327 end328 def verify_visible(timeout=$element_timeout)329 wait("Element #{self} was not visible after #{timeout} seconds.", timeout).until {330 element.displayed?331 }332 $logger.info "Verified #{self} is visible"333 self334 end335 def verify_text(value, timeout=$element_timeout, exact_match = false)336 wait("Element #{self} text was not correct after #{timeout} seconds. Expected '#{value}' Got '#{element.text}'",timeout).until {337 if exact_match338 element.text == value.to_s339 else340 element.text.include? value.to_s341 end342 }343 $logger.info "Verified #{self} text contains '#{value}'"344 self345 end346 def displayed_items347 visible_item_collection = []348 self.each do |current_element|349 if current_element.displayed?350 visible_item_collection << current_element351 end352 end353 visible_item_collection354 end355 def verify_attribute(name, value, timeout=$element_timeout, exact_match = false)356 wait("Element #{self} attribute #{name} was not correct after #{timeout} seconds. Expected '#{value}' Got '#{element.attribute(name)}' #{self.html}",timeout).until {357 if element.attribute(name).nil?358 raise "Element #{self} was not correct. Does not contain attribute #{name}: #{self.html}"359 end360 if exact_match361 element.attribute(name) == value.to_s362 else363 element.attribute(name).include? value.to_s364 end365 }366 $logger.info "Verified #{self} attribute '#{name}' contains '#{value}'"367 self368 end369 def verify_text_not(value, timeout=$element_timeout, exact_match = false)370 wait("Element #{self} text was incorrect after #{timeout} seconds. Expected '#{element.text}' to NOT include '#{value}'", timeout).until {371 if exact_match372 not element.text == value.to_s373 else374 not element.text.include? value.to_s375 end376 }377 $logger.info "Verified #{self} text is NOT '#{value}'"378 self379 end380 def verify_attribute_not(name, value, timeout=$element_timeout, exact_match = false)381 wait("Element #{self} value was not correct after #{timeout} seconds. Expected '#{value}' != '#{element.attribute(name)}'", timeout).until {382 if element.attribute(name).nil?383 return self384 end385 if exact_match386 element.attribute(name) != value.to_s387 else388 not element.attribute(name).include? value.to_s389 end390 }391 $logger.info "Verified #{self} #{name} is not '#{value}'"392 self393 end394 def verify_not_visible(timeout=$element_timeout)395 wait("Element #{self} value was still visible after #{timeout} seconds.", timeout).until {396 not self.displayed?397 }398 $logger.info "Verified #{self} is not visible"399 self400 end401 def verify_present(timeout=$element_timeout)402 wait("Element #{self} not present in #{timeout} seconds", timeout).until{403 self.exists?404 }405 $logger.info "Verified #{self} is present"406 self407 end408 def verify_not_present(timeout=$element_timeout)409 wait("Element #{self} value was still present after #{timeout} seconds.", timeout).until {410 not self.exists?411 }412 $logger.info "Verified #{self} is not present"413 self414 end415 def wait_until(timeout=$element_timeout, message=nil, &block)416 wait(message, timeout).until &block417 self418 end419 def verify_value(value, timeout = $element_timeout, exact_match = false)420 wait("Element #{self} value was not correct after #{timeout} seconds. Expected '#{value}' Got '#{self.value}' : #{element.html}", timeout).until{421 if exact_match422 self.value == value.to_s423 else424 self.value.include? value.to_s425 end426 }427 $logger.info "Verified #{self} value is not '#{value}'"428 self429 end430 def verify_value_not(value, timeout = $element_timeout, exact_match = false)431 wait("Element #{self} value was still #{value} after #{timeout} seconds. #{element.html}", timeout).until{432 if exact_match433 self.value == value.to_s434 else435 self.value.include? value.to_s436 end437 }438 $logger.info "Verified #{self} value is not '#{value}'"439 self440 end441 def verify_count_greater_than(num, timeout= $element_timeout)442 wait("#{self} Expected more than #{num} elements after #{timeout} seconds. Got '#{self.count}'", timeout).until{443 count = self.count444 newcount = num.to_i445 count > newcount446 }447 $logger.info "Verified #{self} count is greater than '#{num}'"448 self449 end450 def verify_count_less_than(num, timeout= $element_timeout)451 wait("#{self} Expected less than #{num} elements after #{timeout} seconds. Found #{self.count}", timeout).until{452 self.count < num.to_i453 }454 $logger.info "Verified #{self} count is less than '#{num}'"455 self456 end457 def verify_count(num, timeout= $element_timeout)458 wait("Expected #{num} elements #{self} after #{timeout} seconds. Got '#{self.count}' : #{element.html}", timeout).until{459 self.count == num.to_i460 }461 $logger.info "Verified #{self} count is '#{num}'"462 self463 end464 def verify_count_not(num, timeout= $element_timeout)465 wait("Still found #{self.count} elements #{self} after #{timeout} seconds. : #{element.html}", timeout).until{466 self.count != num.to_i467 }468 self469 $logger.info "Verified #{self} count is not '#{num}'"470 end471 def verify_html(value, timeout=$element_timeout, exact_match = false)472 wait("Element #{self} html was not correct after #{timeout} seconds. Expected '#{value}' Got '#{element.html}'",timeout).until {473 if exact_match474 element.html == value.to_s475 else476 element.html.include? value.to_s477 end478 }479 $logger.info "Verified #{self} html contains '#{value}'"480 self481 end482 def verify_html_not(value, timeout = $element_timeout, exact_match = false)483 wait("Element #{self} html was still #{value} after #{timeout} seconds. #{element.html}", timeout).until{484 if exact_match485 self.html != value.to_s486 else487 not self.html.include? value.to_s488 end489 }490 $logger.info "Verified #{self} html doesn't contain '#{value}'"491 self492 end493 def verify_style(attribute, value, timeout = $element_timeout, exact_match = false)494 wait("Element #{self} style '#{attribute}' was not correct after #{timeout} seconds. Expected '#{value}' Got '#{element.css_value(attribute)} #{self.html}'",timeout).until {495 if exact_match496 element.css_value(attribute) == value.to_s497 else498 element.css_value(attribute).include? value.to_s499 end500 }501 $logger.info "Verified #{self} style #{attribute} contains '#{value}'"502 self503 end504 def verify_style_not(attribute, value, timeout = $element_timeout, exact_match = false)505 wait("Element #{self} style '#{attribute}' was still '#{value} 'after #{timeout} seconds.", timeout).until{506 if exact_match507 self.css_value(attribute) != value.to_s508 else509 not self.css_value(attribute).include? value.to_s510 end511 }512 $logger.info "Verified #{self} style #{attribute} doesn't contain '#{value}'"513 self514 end515 def verify_selected(timeout = $element_timeout)516 wait("Element #{self} was not selected after #{timeout} seconds. #{self.html}'",timeout).until {517 element.selected?518 }519 $logger.info "Verified #{self} is selected"520 self521 end522 def verify_not_selected(timeout = $element_timeout)523 wait("Element #{self} was still selected after #{timeout} seconds. #{self.html}", timeout).until{...
abstract_page.rb
Source:abstract_page.rb
...66 return HomePage.new(@@driver)67 end68 def checkText (expected_text, expected_size, expected_family, expected_color)69 assert_equal(@@textElement.text, expected_text)70 assert_equal(@@textElement.css_value("font-size"), expected_size)71 assert_equal(@@textElement.css_value("font-family"), expected_family)72 assert_equal(@@textElement.css_value("color"), expected_color)73 return HomePage.new(@@driver)74 end75 def waitBeforeClick (selector, element, delay)76 wait = Selenium::WebDriver::Wait.new(:timeout => delay)77 @input = wait.until {78 element = @@driver.find_element(selector => element)79 element if element.displayed?80 }81 @input.click82 end83 def clickOnElement (selector_type, element_path)84 @@driver.find_element(selector_type => element_path).click85 end86 def selectExpenses (option1, *more)87 wait = Selenium::WebDriver::Wait.new(:timeout => 50) # Wait until Done button is displayed; this means that all other options are displayed88 doneBtn = wait.until {89 element = @@driver.find_element(:xpath,"//*[@id=\"inner-content\"]/div[1]/div[1]/div[14]/div[3]/span")90 element if element.displayed?91 }92 @@driver.find_element(:xpath,"//span[contains(text(), \"" + option1 + "\")]").click93 sleep 194 for i in 0..more.length #95 unless more[i].nil?96 #puts @@driver.find_element(:xpath,"//span[contains(text(), \"" + more[i].to_s + "\")]").text97 @@driver.find_element(:xpath,"//span[contains(text(), \"" + more[i].to_s + "\")]").click98 sleep 199 end100 end101 end102 def checkSelectedExpensesInFinalList(condition, msg)103 assert(condition, msg)104 end105 def getTextString(selector, element)106 return @@driver.find_element(selector => element).text107 end108 def compareElementsOfArrays (a, b)109 assert(a.size == b.size, "The number of selected expenses does not match the number of displayed expenses in Where Can You Cut Costs screen")110 a.each { |i| b.each { |j| a -= [i] if j.start_with?(i) } }111 assert(a.each { |i| b.each { |j| a -= [i] if j.start_with?(i) } }.empty? , "Selected expenses did not match ")112 end113 def rescue_exceptions114 begin115 yield116 rescue Selenium::WebDriver::Error::NoSuchElementError117 false118 rescue Selenium::WebDriver::Error::StaleElementReferenceError119 false120 end121 end122 def is_displayed?(locator = {})123 rescue_exceptions { @@driver.find_element(locator).displayed? }124 end125 def check7Lollipops126 #assert_equal(@@driver.find_element(:id, "slider-vertical-7").displayed?, true)127 expect(is_displayed?(id: 'slider-vertical-7')).to eql true128 expect(is_displayed?(id: 'slider-vertical-8')).to eql false129 end130 def submitButtonsColor (button_path, expected_color)131 @@button = @@driver.find_element(:css => button_path)132 expect(is_displayed?(css: button_path)).to eql true133 assert(@@button.css_value("background").include?(expected_color), "YES")134 end135 def submitButtonsHidden (button1_path, button2_path)136 expect(is_displayed?(css: button1_path)).to eql false137 expect(is_displayed?(css: button2_path)).to eql false138 end139 def screenShot(screenShotName)140 @@driver.save_screenshot("/var/lib/jenkins/jobs/AtWork_AutomatedTest/workspace/src/screenshots/"+screenShotName)141 # @@driver.save_screenshot("./screenshots/"+screenShotName)142 end143end...
sanity.rb
Source:sanity.rb
...42 # Find the download link at the top.43 @driver.find_element(:css, ".downloads a.download-link")44 # Dart is new, yet familiar carousel.45 examples = @driver.find_elements(:css, ".dart-new-language .item")46 expect("block", examples[0].css_value("display"),47 "First new, yet familiar example should be shown.")48 expect("none", examples[1].css_value("display"),49 "Second new, yet familiar example should be hidden.")50 carousel_links = @driver.find_elements(:css, ".dart-new-language .carousel-indicators li")51 carousel_links[1].click52 Selenium::WebDriver::Wait.new.until {53 examples[0].css_value("display") == "none"54 }55 expect("none", examples[0].css_value("display"),56 "First new, yet familiar example should be hidden.")57 expect("block", examples[1].css_value("display"),58 "Second new, yet familiar example should be shown.")59 end60 def test_darrrt61 # Click "Get Started".62 @driver.find_element(:css, ".nav li:first-child a").click63 Selenium::WebDriver::Wait.new.until {64 iframe = @driver.find_element(:css, ".nav li:first-child ul a:first-child")65 }66 # Click "Learn Dart in Minutes".67 @driver.find_element(:css, ".nav li:first-child ul a:first-child").click68 Selenium::WebDriver::Wait.new.until {69 iframe = @driver.find_element(:css, ".running-app-frame")70 }71 # Open the top iframe....
get.rb
Source:get.rb
...71 return att72 end73 74 #method to get the background color75 def return_background_color_on_mouse_over(css_value, seconds_wait)76 77 wait_for(seconds_wait) { @driver.find_element(:css,css_value).displayed? }78 79 element = @driver.find_element(:css,css_value) 80 value = element.css_value('background-color')81 puts value82 83 @driver.action.move_to(element).perform84 sleep 385 86 value = element.css_value('background-color')87 puts value 88 89 return value90 end9192 93 #method to get the HTTP header information94 def return_HTTP_header(url)95 puts "inside getHeader"96 response = Net::HTTP.get_response(URI.parse(url))97 puts "this is reponse: "98 puts response99 #response.header100 response.header.each_header {|key,value| puts "#key = #{value}" }
...
rubyci-screenshot.rb
Source:rubyci-screenshot.rb
...40}41END42elem = driver.find_elements(:tag_name, "table")[0]43loc = elem.location44width = elem.css_value("width").to_i45height = elem.css_value("height").to_i46driver.save_screenshot("rubyci.orig.png")47crop = "#{ width }x#{ height }+#{ loc.x }+#{ loc.y }"48system("convert", "rubyci.orig.png", "-crop", crop, "rubyci.png")49unless SLACK_API_TOKEN50 puts "SLACK_API_TOKEN is not specified"51 exit52end53filename = Time.now.strftime("rubyci-%Y%m%d.png")54open("rubyci.png", "rb") do |f|55 data = [56 ["token", SLACK_API_TOKEN],57 ["channels", CHANNEL_ID],58 ["filename", filename],59 ["filetype", "png"],...
view_android_using_selenium.rb
Source:view_android_using_selenium.rb
...19 @driver.get(@base_url + "/")20 # store the image element into a variable21 image_element = @driver.find_element(:css, ".iphone-first-screenshot")22 # verify that iphone is selected by default23 #verify { image_element.css_value('background-image').should =~ /^[\s\S]*iphone[\s\S]*$/}24 raise "I don't believe 'iphone' is selected by default" unless image_element.css_value('background-image').include?("iphone") 25 # click the android icon26 @driver.find_element(:css, "li.android").click27 # verify that android is now selected 28 raise "I don't believe an 'android' image is selected" unless image_element.css_value('background-image').include?("android")29 end30 31 #def element_present?(text)32 #@driver.is_text_present(text)33 #true34 #rescue Selenium::WebDriver::Error::NoSuchElementError35 #false36 #end37 38 def verify(&blk)39 yield40 rescue ExpectationNotMetError => ex41 @verification_errors << ex42 end...
zadanie_10_attribute.rb
Source:zadanie_10_attribute.rb
...25puts regular_price.attribute('textContent') == '$20' ? 'Valid regular price' : 'Invalid regular price'26puts campaign_price.attribute('textContent') == '$18' ? 'Valid campaign price' : 'Invalid campaign price'2728c = Color::RGB.from_html('666')29puts "rgba(#{c.red.to_i}, #{c.green.to_i}, #{c.blue.to_i}, 1)" == regular_price.css_value('color') ? 'Regular price color valid' : 'Regular price color invalid'30puts regular_price.css_value('text-decoration').include?('line-through') ? 'Regular price has strike-through' : 'Regular price does not have strike-through'31c = Color::RGB.from_html('c00')32puts "rgba(#{c.red.to_i}, #{c.green.to_i}, #{c.blue.to_i}, 1)" == campaign_price.css_value('color') ? 'Campaign price color valid' : 'Campaign price color invalid'33puts regular_price.css_value('font-weight').to_i == 400 ? 'Campaign price bold' : 'Campaign price not bold'34
...
css.rb
Source:css.rb
...3dr = Selenium::WebDriver.for :chrome4file_path = 'file:///' + File.expand_path(File.join('.', 'css.html'))5dr.get file_path6link = dr.find_element(id: 'tooltip')7puts link.css_value(:color)8puts dr.find_element(:tag_name, 'h3').css_value('font')9dr.quit()...
css_value
Using AI Code Generation
1element = driver.find_element(:name, 'q')2wait.until { driver.title.downcase.start_with? "selenium webdriver" }3element = driver.find_element(:name, 'q')4wait.until { driver.title.downcase.start_with? "selenium webdriver" }5element = driver.find_element(:id, 'resultStats')6element.css_value("font-size")7element = driver.find_element(:name, 'q')8wait.until { driver.title.downcase.start_with? "selenium webdriver" }9element = driver.find_element(:id, 'resultStats')10element.css_value("font-size")11element = driver.find_element(:name, 'q')12wait.until { driver.title.downcase.start_with? "selenium webdriver" }13element = driver.find_element(:id, 'resultStats')14element.css_value("font-size")
css_value
Using AI Code Generation
1 @driver.get(@base_url + "/")2 @driver.find_element(:id, "gbqfq").clear3 @driver.find_element(:id, "gbqfq").send_keys "selenium"4 @driver.find_element(:id, "gbqfb").click5 @driver.find_element(:link, "Selenium - Web Browser Automation").click6 assert_equal "Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) be automated as well.", @driver.find_element(:css, "p").text7 assert_equal "rgba(0, 0, 0, 0.87)", @driver.find_element(:css, "p").css_value("color")
css_value
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = driver.find_element(:name, 'q')3element = driver.find_element(:name, 'q')4puts element.css_value('font-size')5element = driver.find_element(:name, 'q')6element = driver.find_element(:name, 'q')7puts element.css_value('font-size')8element = driver.find_element(:name, 'q')9element = driver.find_element(:name, 'q')10puts element.css_value('font-size')11element = driver.find_element(:name, 'q')12element = driver.find_element(:name, 'q')13puts element.css_value('font-size')
css_value
Using AI Code Generation
1color = driver.find_element(:id, 'lst-ib').css_value('color')2font_size = driver.find_element(:id, 'lst-ib').css_value('font-size')3rgba(0, 0, 0, 1)4find_element(:css, css_selector)5find_elements(:css, css_selector)6puts element.attribute('id')7element = driver.find_element(:css, '.gb_P')8puts element.attribute('class')9element = driver.find_element(:css, '[name="q"]')10puts element.attribute('name')11element = driver.find_element(:css, '[title="Google Search"]')12puts element.attribute('title')
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!!