0

Can we develop selenium webdriver scripts using javascript only. If yes what are the advantages of using javascript instead of using java or C# or any language.? In what scenario we should consider Javascript over other languages?

Thank you.

asked Apr 11, 2016 at 13:37

2 Answers 2

2

There are numerous Javascript frameworks written on top of the JS selenium bindings (webdriver.io, nightwatch, protractor).

Benefits of JS over C# or Java

  • Less boilerplate
  • Grunt or gulp for build automaton> maven, msbuild or even gradle
  • Better integration with front-end frameworks (protractor)
  • JS is generally used in all web projects so it works as a universal language that all devs can understand.
  • NPM for dependency management and the massive number of libraries in it that may drastically reduce your workload.

Drawbacks

  • The Java bindings have more documentation / resources available to debug issues.
  • Speed (I find using the AjaxPageFactory in Java to be a lot faster than protractor and I have not found an equivalent in Javascript)
  • Promises can be complicated coming from Java or C#.
  • No support for Microsoft Edge currently.

As far as specific use cases if you use Angular heavily on the front-end Protractor is a tool designed specifically for functional testing of angular and should be used over C# or Java. Protractor can be used with frameworks such as React.js but it wasn't designed for it and you may need to include a lot of waitForElement type code.

A few things I have found really nice about protractor specifically is the configuration for more comprehensive multi-browser testing. To set this up in Java or C# involves a lot of configuration and in Protractor it could be as simple as make two changes to your conf.js file. I also find myself using a lot of grunt plugins to set up and tear down my tests which are very simple to configure.

I would recommend using Babel.js so you can utilize the es2015 JS syntax which makes the transition from Java or C# simpler due to the inclusion of classes and I personally find it much more cleaner for writing page objects.

One thing to be aware of is a lot of simple actions in Selenium for Java and C# are more complicated in Javascript because most actions return promises.

Java Version

int previousNumberOfItems = driver.findElements(By.className(".item")).size();
driver.findElements(By.cssSelector(".addItemButton")).click();
int currentNumberOfItems = driver.findElements(By.className(".item")).size();
assert.AssertTrue(currentNumberOfItems .contentEquals(previousNumberOfItems +1));

Protractor (JS) Version

element.all(by.className(".item")).count().then(function(number){
 element(by.css(".addItemButton")).click();
 expect(element.all(by.className(".item")).count()).toBe(number+1);
});

I can't really speak for nightwatch or webdriver.io they may be much better for testing non-angular apps using Javascript.

answered Apr 12, 2016 at 10:13

8 Comments

I'm new JS but earlier worked on selenium with Java. Can you please let me know the scope of JS i should be familiar with for selenium and which editor to be used for automation.
A basic javascript tutorial should get you up to speed fast especially if you already know Java and Selenium. There are a lot of ways to structure your protractor page objects but I think the constructor pattern is the easiest. samselikoff.com/blog/some-Javascript-constructor-patterns . Then it helps to learn a little about modules and promises but only the bare minimum is necessary for protractor. Last familiarizing yourself with NPM and Grunt / or Gulp would be really helpful.
thank you for info. Does Selenium webdriver and webdriverJS have same APIs? I'm using net-beans now but it does not give any intelligence as Eclipse would give for webdriver APIs.
I don't use IDE's when writing Javascript so I am not the best person to ask. Sounds like a reasonable new question to submit. But if you look at the protractor documentation a lot of the webdriverJS api is there and it re-directs you to that documentation for parts of the code not specific to protractor. I do not think that alone would help the intellisense (intelligence) in netbeans. angular.github.io/protractor/#/api?view=webdriver.By.className
I think I may have misread your question. Javascript is a dynamic language so auto-completion will not work as well as Java or C#. I think Webstorm is the only IDE that will have decent auto-completion. Typescript would definitely be able to do that but I wouldn't recommend learning it until you can write selenium in javascript pretty well because the documentation is not great and there isn't a lot of stackoverflow answers.
|
1

Yes you can use Nodejs protractor Jasime framework with WebDriver.

Here is some link:

Webdriver java script binding

Angularjs Jasmine

Sadik Ali
1,20512 silver badges27 bronze badges
answered Apr 11, 2016 at 20:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.