How difficult is to rewrite a Java Selenium test in C#?

not so bad

I recently wrote an automated test that

  • opens a site
  • searches for a keyword
  • selects a result on results page
  • enables an alert on details page
  • gets api calls executed by details page when enabling the alert
  • gets data from the response of the api calls
BrowserMob is ideal for getting the api calls but i could not get it to work in C# with https requests.

So i wrote the code in Java which worked very well.

The development team insisted though that the automation code is in C# since this is the development language.

So, after figuring out a way of not using BrowserMob (I did not need to get those API calls because the information I was after was also in a cookie), I started to convert the Java code to C#.

How difficult can it be to convert Java code to C#?

It turned out that it was very easy.

 

These were the major differences for my test so it runs in Visual Studio, C# instead of Eclipse and Java:

 

1. Selenium interfaces are named differently (WebDriver is IWebDriver in C#, WebElement is IWebElement)

 

2. There is no CamelCase notation

 

3. String formatting is slightly different and uses {} instead of %

 

4. Writing text to files is a bit different

 

5. const is used instead of final static for constants

 

6. the collection returned by driver.FindElements() is not a list but a readonly collection

 

7. the equivalent of IllegalArgumentException is ArgumentException

 

8. all Selenium methods are minimally different

 

9. there are no expected conditions but the alternative is better and more flexible

 

10. Collection methods have different names

 

11. String in Java is string in C#

 

12. packages in Java are namespaces in C#

 

13. nUnit is similar to jUnit with minor differences

C# and Java are so similar

That it makes me believe that coding in C# after coding in Java is like driving a Honda after driving a Ford.

There are differences, some important but most minimal.

Knowing one well should make learning the other one very easy.

And having 2 languages in your toolbox just makes you more employable.

Leave a comment