April 13, 2016

Elemental Selenium code examples are being written in Java!

I love reading Elemental Selenium, automation development tips written by Dave Haeffner -- who I have written about in this blog.

Archive of Dave's Weekly Newsletter of Automation Tips!

The only problem has been that Dave's language of choice for the code examples is in Ruby. Since I only have been just getting back into coding, I have had difficulty converting the code into Java, what I am using on the workplace.

There is hope! If you take a look on Dave's ( @TourDeDave ) GitHub site, you can find the corresponding Elemental Selenium Tips source code.

Sourcecode for examples in the Elemental Selenium Tips

It looks like around a third of the seventy examples have been converted to Java.

Let's compare the first tip, uploading a file, about how to setup and teardown a browser.

Setup/ TeardownRuby:

 # filename: upload.rb  
 require 'selenium-webdriver'  
 require 'rspec/expectations'  
 include RSpec::Matchers  
 def setup  
  @driver = Selenium::WebDriver.for :firefox  
 def teardown  
 def run  

Setup/ Teardown: Java:
public class Upload {  
   WebDriver driver;  
   public void setUp() throws Exception {  
     driver = new FirefoxDriver();  
   public void tearDown() throws Exception {  

... You know, they look pretty similar. Let's compare the Ruby version Dave has in his original tip to the one that he has in GitHub.

File Upload Test: Ruby:
 run do  
  filename = 'some-file.txt'  
  file = File.join(Dir.pwd, filename)  
  @driver.get 'http://the-internet.herokuapp.com/upload'  
  @driver.find_element(id: 'file-upload').send_keys file  
  @driver.find_element(id: 'file-submit').click  
  uploaded_file = @driver.find_element(id: 'uploaded-files').text  
  expect(uploaded_file).to eql filename  

File Upload TestJava:
   public void uploadFile() throws Exception {  
     String filename = "some-file.txt";  
     File file = new File(filename);  
     String path = file.getAbsolutePath();  
     String text = driver.findElement(By.id("uploaded-files")).getText();  
     assertThat(text, is(equalTo(filename)));  

Ruby and Java are as different as night and day. If you were a newly minted software developer, there would be no way to translate from Ruby, where much of the complexities are hidden from view, to Java, where everything is all out in the open.

Wondering what the AssertThat is? That is Hamcrest. Instead of using TestNG's assertEquals(actualValue, expectedValue) this is a bit more readable.

I'm glad that the code examples from Ruby to Java. This would help former manual testers like myself not just learn automation development, but learn Java development.

Hrm... it looks like Dave is looking for people to translate Ruby to Java for some of his code examples:
"They're currently only available in Ruby. But for each one that gets converted to a new programming language, I'll do a full write-up for it.
"To submit your entry:
  • "Fork the repo
  • "Choose a tip to port
  • "Add a folder to it for the language you want (e.g., javascript, java, csharp, php, python, etc.)
  • "Write the code example
  • "Submit a pull request"
... Work has been quite busy, bleeding into my free time... but maybe I can make this my next weekend project?

- T.J. Maher
 Sr. QA Engineer, Fitbit-Boston

// Software QA Engineer since August 1996
// Automation Developer: [ 1 ] year and counting

No comments: