Book Excerpt - Science of Selenium - Ch-7 Selenium Coding with JavaScript & Ruby
Kalilur Rahman
Director @ Novartis | IT Strategy, IT Management, Business Transformation
Dear All
Using this forum to share some excerpts to raise the interest levels of the book I wrote and published through BPB Publications. I will share some excerpts every week to get your inputs and suggestions for future work.
This week, I am focusing on the Chapter - 7 Selenium Coding with JavaScript & Ruby.
Chapter 7 covers how to perform Selenium automation using programming languages such as Ruby and JavaScript.
Chapter 7
Selenium Coding with Other Languages (JavaScript, Ruby)
“The first rule of any technology used in a business is that automation applied to an efficient operation will magnify the efficiency. The second is that automation applied to an inefficient operation will magnify the inefficiency.
– Bill Gates”
Introduction
Until now, we have looked at Selenium automation using the two most popular languages of choice, that is, Java and Python. In this chapter, we willlook at some of the concepts using other programming languages such as JavaScript and Ruby. We will also try to cover some programming examples in other languages (PHP, C#, etc.) that Selenium supports. Most of the concepts covered in this chapter will be a replication of the concepts covered earlier.
Structure
This chapter will cover programming examples in JavaScript, Ruby and other languages so that you can understand and explore the following concepts:
● Launch and closure of browsers
● Browser level automation tasks, including –
- URL/Web page navigation and URL information gathering
- Page-level automation like page info gathering, element identification and data capturing
- CSS and XPathelement locators, selectors and locator ID customization
- Management of Document Object Model (DOM)
- Simulation of screen sizes
- UI element level automation, covering the following points –
- State inquiry, event handling, asynchronous interactions
- UI element styling, moving between frames and windows,
- handling pop-ups and windows
- History and locations, handling cookies, dragging and dropping Changing user agents
- Fetching data from forms
- Filling and submitting forms
....
....
....
INTENTIONALLY LEFT BLANK
....
....
....
How to Install and use Node.JS or JavaScript
TIP You can download the Node.js installers for your development from the following sites: ● https://nodejs.org/en/ – The site would give you the ideal version for your operating system. ● https://nodejs.org/en/download/ –The site would give you a choice to pick the installer or downloadable version suitable for your platform including add-ons, available in the downloader site. You may download the latest version of your choice for installation. The installation process is very straightforward and self-explanatory. Since at the time of writing this book, the Selenium Alpha-5 version was the most apt choice, we have picked this up. After the installation of Node.JS, you need to install Selenium- WebDriver to run your tests. This can be carried out by opening the Node.js command prompt and running the following command: npm install selenium-webdriver As a next step, you need to ensure you have a good JavaScript- friendly integrated development environment. There are plenty of choices. Some of the top ones to consider are as follows: IntelliJ IDEA, MS Visual Studio Code, Komodo IDE, Koding, Eclipse with Node.JS Plug-in - nodeclipse, Atom and Brackets The programs used in this book were created using MS Visual Studio Code and Atom IDEs.
How to Install and use RUBY
TIPS You can download the Ruby installers for your development from the following sites: ● https://rubyinstaller.org/ – The sitewould give you a choice to pick the installer or downloadable version suitable for your platform including add-ons, available in the downloader site. You may download the latest version of your choice for installation. The installation process is very straightforward and self-explanatory. Since at the time of writing this book, the Selenium Alpha-5 version was the most apt choice, we have picked this up. If you want to install any other gems (or libraries), you can use the gem install command. If you want to use the available developer kits or any other Ruby Gems (libraries for simple references), install them as well. ruby dk.rbinit and ruby dk.rb install After the installation of Ruby, you need to install Selenium-WebDriver to run your tests. This could be carried out by opening the Ruby command prompt and running the following command: gem install selenium-webdriver As a next step, you can choose to install any of the following libraries, if you want to test Selenium Grid, headless browser or perform Behaviour Driven Design (BDD) based testing using Cucumber etc. gem install rspec/gem install selenium-cucumber/gem install selenium-phantomjs/gem install selenium-grid As a next step, you need to ensure you have a good Ruby-friendly Integrated development environment. There are plenty of choices. MS Visual Studio Code, Eclipse with Ruby-dltk, Atom, VIM, EMACS and JetBrains RubyMine are some The programs used in this book were created using MS Visual Studio Code and RubyMine IDEs.
....
....
....
INTENTIONALLY LEFT BLANK
....
....
....
Selenium JavaScript Code Example & RUBYCode Example
....
....
....
INTENTIONALLY LEFT BLANK
....
....
....
Headless browser testing – Ruby
Similar to the JavaScript code in the preceding section, the code for Ruby is almost similar toa headless program. As a second method, we will use a class headless by instantiating the object using the Headless.new and destroying it before the program is closed using the Headless.destroy functions. The rest of the code would exactly remain the same as the WebDriver program. In the program below, the use of the headless class commented using # tag, which covers both the type of headless program outputs.
require 'selenium-webdriver' require 'rspec/expectations' def setup options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless') options.add_argument('--disable-gpu') @driver = Selenium::WebDriver.for :chrome, options:options end def teardown @driver.quit end def run setup yield teardown end run do @driver.get "https://sites.google.com/view/crackingseleniuminterview/ home" puts(@driver.title) @driver.save_screenshot('Ch_7_Prog_7_Ruby_Headless.png') end
....
....
....
INTENTIONALLY LEFT BLANK
....
....
....
Conclusion
In this chapter, we have covered the key essential features of WebDriver automation using the JavaScript and Ruby programming languages. In the next chapter, we shall look at the conceptual approach, which is important for building a test automation framework using Selenium such as POM. In addition, we will also look at how we can bring the entire components of the test automation framework into onepicture. Having a strong grasp of the basic components of test automation, how to interweave them together to build a solid framework, having a strong approach for test automation and being flexible to accommodate the futuristic changes will be a key to successful implementation. In the next chapter, we will discuss some of the advanced concepts such as how to establish a test automation framework and how to go about designing a solid approach for test automation.
Questions
- How will you open a web browser using JavaScript and Selenium WebDriver?
- What is the use of WebElementPromise in JavaScript?
- What is the use of ThenableWebDriver Class?
- How will you use ActionChains in the Ruby programming language?
- What is the difference between JavaScript and Ruby when it comes to the automation of Selenium WebDrivers?
- Where will you use Ruby and Python for web automation? What are the pros and cons of one over another?
- What are the key differences between Java and JavaScript when it comes to testing automation
- How will you automate headless browser testing using Ruby and JavaScript in Selenium?
Conclusion
Hope you enjoyed the excerpts from Chapter-7. What are your comments?
Book Availability
The book is available at the following sites
- Australia ==> Amazon ==> https://www.amazon.com.au/dp/B082KFY17B
- Austria ==> Amazon ==> https://www.amazon.de/dp/B082KFY17B
- Brasil ==> Amazon ==> https://www.amazon.com.br/dp/B082KFY17B
- Canada ==> Amazon ==> https://www.amazon.ca/dp/B082KFY17B
- Czech Republic ==> Amazon ==> https://www.amazon.de/dp/B082KFY17B
- France ==> Amazon ==> https://www.amazon.fr/dp/B082KFY17B
- Germany ==> Amazon ==> https://www.amazon.de/dp/B082KFY17B
- India ==> Amazon ==> https://www.amazon.in/dp/B082KFY17B
- India ==> Amazon ==> https://www.amazon.in/dp/B082KFY17B/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1
- India ==> Amazon ==> https://www.amazon.in/Science-Selenium-Automation-Framework-English-ebook/dp/B082KFY17B
- Italy ==> Amazon ==> https://www.amazon.fr/dp/B082KFY17B
- Japan ==> Amazon ==> https://www.amazon.co.jp/dp/B082KFY17B
- Japan ==> Amazon ==> https://www.amazon.co.jp/Science-Selenium-Automation-Framework-English-ebook/dp/B082KFY17B
- Mexico ==> Amazon ==> https://www.amazon.com.mx/dp/B082KFY17B
- Spain ==> Amazon ==> https://www.amazon.es/dp/B082KFY17B
- Switzerland ==> Amazon ==> https://www.amazon.de/dp/B082KFY17B
- UK ==> Amazon ==> https://www.amazon.co.uk/dp/B082KFY17B
- USA ==> Amazon ==> https://www.amazon.com/dp/B082KFY17B
- USA ==> Amazon ==> https://www.amazon.com/Science-Selenium-Automation-Framework-English-ebook/dp/B082KFY17B
- USA ==> Barnes & Noble ==> https://www.barnesandnoble.com/w/science-of-selenium-kalilur-rahman/1135502525?ean=2940163963881
- Brasil ==> Livrariacultura ==> https://www3.livrariacultura.com.br/science-of-selenium-2012821784/p
- Canada ==> Indigo ==> https://www.chapters.indigo.ca/en-ca/books/science-of-selenium/9789389423242-item.html
- Canada ==> Indigo ==> https://www.chapters.indigo.ca/fr-ca/livres/science-of-selenium/9789389423242-article.html
- Germany ==> WetBild ==> https://www.weltbild.de/artikel/ebook/science-of-selenium_26964037-1
- Germany ==> WetBild ==> https://www.weltbild.de/artikel/ebook/science-of-selenium_26964037-1
- Global ==> Google Play ==> https://play.google.com/store/books/details/Kalilur_Rahman_Science_of_Selenium?id=-ofCDwAAQBAJ
- Global ==> Review Site ==> https://www.goodreads.com/book/show/49289577-science-of-selenium
- Global ==> Scribd ==> https://www.scribd.com/book/439434766/Science-of-Selenium
- Global ==> Textbookx ==> https://www.textbookx.com/book/Science-of-Selenium-Master-Skills-Needed-to-Become-a-Top-Test-Automation-Guru-with-Easytofollow-Selenium-Examples/9789389423242/
- Holland ==> Bol ==> https://www.bol.com/nl/p/science-of-selenium/9200000126647725/
- India ==> BPB Publications ==> https://bpbonline.com/products/science-of-selenium-master-web-ui-automation-and-create-your-own-test-automation-framework
- India ==> PayTM Mall ==> https://paytmmall.com/science-of-selenium-RKC0003512361_882733-pdp
- India ==> SapnaOnline ==> https://www.sapnaonline.com/books/science-selenium-master-web-ui-kalilur-rahman-9389423244-9789389423242?position=2&searchString=science%20of%20selenium
- India ==> Snapdeal ==> https://www.snapdeal.com/product/science-of-selenium/626675025728
- Japan ==> Rakuten ==> https://books.rakuten.co.jp/rk/7a977206400b37fb90e0057fc90225a7/
- Portugal ==> FNAC ==> https://www.fnac.pt/livre-numerique/a7376163/Science-of-Selenium
- Saudi Arabia ==> Google Play ==> https://play.google.com/store/books/details/Science_of_Selenium_Master_Web_UI_Automation_and_C?id=-ofCDwAAQBAJ&hl=ar
- Saudi Arabia ==> VitalSource ==> https://www.vitalsource.com/sa/en-us/products/science-of-selenium-master-skills-needed-to-kalilur-rahman-v9789389423242
- Serbia ==> Kombib ==> https://knjige.kombib.rs/science-of-selenium-master-web-ui-automation-and-create-your-own-test-automation-framework
- Spain ==> FNAC ==> https://www.fnac.es/livre-numerique/a7147532/Science-of-Selenium
- Sweden ==> BOKUS ==> https://www.bokus.com/bok/9789389423242/science-of-selenium/
- USA ==> Tattered Cover ==> https://www.tatteredcover.com/book/9789389423242
- USA ==> Walmart ==> https://www.walmart.com/ip/Science-of-Selenium-eBook/466432367
Note:- Title Images are created using CANVA tools. Authors of the quote referred where known. Most of the information shared is generic and available in various forms in the Internet. Respective trademarks are owned by corresponding firms. Opinions about tools highlighted are from a personal experience standpoint and in no way reflect the views of my current or past employers or clients.
#WhatInspiresMe #Automation #Selenium #KRPoints #TestAutomation #DevOps #AITesting #NewAgeTesting #MobileTestAutomation
Empowering Brands to Stand Out, Scale Up, and Achieve Sustainable Growth.
5 年Congratulations my dear friend Kalilur Rahman??
Telecom Professional
5 年Great
Telecom Professional
5 年Great
Team Building Expert | LinkedIn Top Voice | Forbes featured | I help executives manage change & foster innovation even in the midst of turbulence ???? ???? Actress ?? Writer ?? ???
5 年That is why wee need to connect the dots and monitor what is happening inside and outside our business.