how to declare a driver in protractor - selenium-webdriver

In selenium with java we declare/initialize variable like
WebDriver driver = new FirefoxDriver();
with ruby
driver = Selenium::WebDriver.for :firefox.
So can any one tell me how can we declare/initialize driver in protractor?
I have seen on protractor website, we need to use capabilities but didn't get how a driver is declared.

Actually there is not need to initialize driver in protractor. It is globally available as browser. So in test class you can directly call it as
browser.get(url);
browser.findElement(by.css('cssSelector));
For non-angular pages you can use browser.driver instead of browser. It will look like this
browser.driver.findElement(by.css('cssSelector));
Please read it properly, it will clear your all basic doubts about Protractor. https://angular.github.io/protractor/#/tutorial

Related

WebDriver was closed, Session ID is null. Using WebDriver after calling quit()?

I had been using Cucumber and WebDriver for a while, just adding Spring Boot for dependency injection. The WebDriver was injected by Spring. The first scenario worked fine, but the second scenario got:
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
I did not use any Cucumber Hook to explicitly close/quit the driver, yet the browser closed after the first and second scenario failed.

which eclispe should be downloaded to automate angular js application

Is it possible to automate Angular JS application by using eclipse? If so, which version should I try?
Also, what are the languages I need to know before doing that.
I downloaded Angular JS from Help-->Eclipse Marketplace-->AngularJS
But somehow I feel like I am unable to connect the dots.
Please suggest me on this.
I'm not quite sure what you want to do, but if you have already installed Angular IDE into Eclipse, that's the most you can do from it. It supports projects, syntax and error handling in Ajs. In comment you've mentioned testing angular js application. Like every other website, you can use JUnit or TestNG automatic tests to verify work of your application. If you dont like approach from Eclipse, you can use Selenium IDE test that runs throught Mozilla Firefox.
On picture above you can enable or disable Angular support for your project.

Trying to run selenium - test on angular site

I try to run a selenium test (using selenium IDE on Firefox) For an angular app.
I followed the instructions here in SO and added something like this:
Command=waitForElementPresent
Target=//h3
But even if there are a lot of H3s generated quite quickly by the app, Selenium does not seem to be able to find any them. (it keeps waiting on the WaitForElementPresent command)
Anything else I have to do to make this work?
I would highly recommend you to use protractor in addition to Selenium. Basically its a Javascript executor which can locate elements by ng tags. I tried to test an angular site with selenium only, but protractor really made my life easier!
If there are are many H3 tags in your application , it would be a good idea to give an unique Id to tags .
like :
<h3 id="someUniqueId"> heading </h3>
which can be accessed by :
Command=waitForElementPresent
Target=id=someUniqueId
If you don't want to make the entire switch to Protractor but you do want to wait for Angular I recommend using Paul Hammants ngWebDriver (Java).
For me the standard selenium implicit wait didn't work as our page is built asyncronously with Angular. I fixed this by writing an actions class in which I waited for Angular (using ngWebDriver's waitForAngularRequestsToFinish()) before carrying out the actions (click, fill, check etc.).

Can angularjs apps be automated with selenium? if yes, why should we use protractor?

I know that we can automate AngularJs apps with Selenium. But we have a separate E2E testing framework that is Protractor for AngularJs apps automation.
Can anyone help me understand why we should use Protractor? Why not Selenium?
Not sure I understand your question. Am I right to assume you'd rather use Selenium - but want to understand what you're missing?
Well - Selenium provides means to automate web browsers - and thus used for automated e2e tests. Selenium API has implementations in several major programming languages - allowing you to write your tests in Java, C#, python, ruby, JavaScript and more.
If you already have a selenium-based e2e testing framework in place - you can use it also for AngularJS web-apps. You can also write the necessary JavaScript scripts that, once ran using the webdriver - will let you do all that Protractor does - but you'll have to do it yourself (just borrow from Protractor source code).
Why is it doable? Because Protractor basically took the JavaScript implementation of Selenium Webdriver and wrapped it in a way that makes your life a bit easier when testing Angular JS web apps.
You can see specific explanations in this old post of mine:
http://testautomation.applitools.com/post/94994807787/protractor-vs-selenium-which-is-easier
I'd say that if you:
1. want to write your test code in JavaScript
2. are focused on mainly Angular JS apps
You might want to consider using Protractor. Again - no magic there. Everything they did is there in their source code - so you can just take your picks if you'd rather stick with selenium.
protractor is an end-to-end browser automation testing framework that works through WebDriverJs which is a javascript selenium webdriver.
Quote from How it works? documentation page:
Selenium is a browser automation framework. Selenium includes the
Selenium Server, the WebDriver APIs, and the WebDriver browser
drivers.
Protractor works in conjunction with Selenium to provide an automated
test infrastructure that can simulate a user’s interaction with an
Angular application running in a browser or mobile device.
Protractor is a wrapper around WebDriverJS, the JavaScript bindings
for the Selenium WebDriver API.
Also see:
Automated e2e testing- WebDriverJS, Jasmine and Protractor
With protractor, you can write e2e tests with JavaScript, the language you write with Angular app.
Also, it has Angular-specific features.
Its element finders wait for Angular's $digest loop and $http to finish. So you'll have less chance to struggle with sleep and timing issues.
You can select elements with some of common directives such as ng-model, ng-repeat, ng-bind and etc. This is somewhat handy because you may have relatively less ids and classes in Angular apps because you need them only for CSS.

selenium2 - java

when i'm using selenium rc i used waitForPageToLoad method but in WebDriver i'm unable to find method for wait. is there any way to achieve this? i know webDriver allows wait For page Load implicitly but it's not working for me. thanks in advance.
WebDriver shouldn't return until the page has loaded, but if this is not working for you e.g. your pages is using AJAX, then you could add your own wait code using wait.until from org.openqa.selenium.support.ui.WebDriverWait.
There's a full example at http://www.qaautomation.net/?p=263

Resources