AngularJS controls are becoming more popular these days due to it’s flexibility of use, less coding, allows single page applications and many more which a developer know much better than us (us as testers :P)…
So with the changing in technology and the way controls are used. as a tester, we also need to upgrade our skills and way of testing the applications…
in AngularJS controls, we are getting some extra html attributes like ng-model, ng-repeater, ng-controller etc, these are not included as locators in
selenium.
if any controls have only these attributes are set, we can’t find the elements with selenium,
There is an alternative given to selenium, Protractor which is built on top of selenium to handle the above mentioned attributes.
Protractor tutorial [using typescript or javascript]
Protractor don’t support java language to write scripts, so the workaround to selenium java for Angulas JS applications is to use the xpath (or CSS Selector) as locator when we are using raw selenium for angularJS controls.
xpath / CSS Selector simply searches for the elements with the tags and attributes irrespective of what technology used to build the site…
while automating angular sites, we need to only take care of identifying the elements which is different than regular web elemenets.., rest automation stuff like framework, reportings are as usual…
let’s see a sample code for testing angularJS controls with raw selenium for Java…
package testPkg; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class AngularSite { public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); driver.get("https://angularjs.org/"); Thread.sleep(1000); //we are about to identify an element with attribute ng-model driver.findElement(By.xpath("//input[@ng-model='yourName']")).sendKeys("xyz"); WebElement fld_Result = driver.findElement(By.xpath("//h1[@class='ng-binding']")); System.out.println(fld_Result.getText()); if (!(fld_Result.getText() == "Hello xyz!")) //returns 0, if equals, so negation { System.out.println("Passed"); } } }
Update – Some times, we get issues with synchronisation with the angular elements [in background, angular elements use heavy javascript], so If you get any kind of issue of using xpath for angular web apps, you can try ngWebdriver with java.
Short link – bit.ly/qav-selangularJS
Thanks for this sample code. It gave me a working to solution on how to work with AngularJS applications. I would have hated to learn Protractor with Jasmine. Personally, I am not a fan of either. Great work.
Thanks for your article. I knew it working with Angular Js on how to work.
I am trying to find the value which appears in text box and element using angular js attributes. Value which is apepearing in text box is neither available as any arrtibute value netiher we can fetch it using getText(); as value is not available there. If id would have been given as attributed i would have used
WebDriver driver = DriverFactory.getDriver();
public String getTextAt(String cssId) {
if (driver instanceof JavascriptExecutor) {
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
return (String) javascriptExecutor.executeScript(“return documenet.getElementById(‘” + cssId + “‘).value;”);
}
return null;
}
This is working fine in the case where id is given but in my case even id is not given. Please suggest what can be used.
Here is the view of HTML/Angular JS
Try this:
WebElement control=driver.findElement(By.id(“some_id”));
control.getAttribute(“value”);
Is this code will work for Angular JS
Yes, you can still use untill you get any synchronisation issue, sometimes you may face timing issues where selenium will run faster than the angular actions, u can add another library ngWebDriver to existing selenium and get some more advantages, refer http://qavalidation.com/2017/10/ngwebdriver-way-automate-angular-apps-selenium-using-java.html/
Nice article