Protractor: A guide to finding buttons using className

As a beginner in web automation testing, I am encountering an issue with locating the login button on a login screen page.

While finding the name and password textfields was easy using by.model, I am struggling to locate the login button with my script.

I have tried element(by.css(.className)) as suggested online but it keeps throwing

NoSuchElementError: No element found using locator: By.cssSelector(".btn btn-lg btn-primary btn-block")
.

Can someone help me figure out what I am doing wrong?

Thank you,

LoginBtn HTML:

<button class="btn btn-lg btn-primary btn-block" type="submit" ng-disabled="loading">login</button>

My code:

describe('Test login', function() {
  var username = element(by.model('formData.email'));
  var password = element(by.model('formData.password'));
  var loginBtn = element(by.css('.btn btn-lg btn-primary btn-block'));


  beforeEach(function() {
    browser.get('some site'); //hidden on purpose
  });

  it('should have a title', function() {
        username.sendKeys(1);
        password.sendKeys(2);
        loginBtn.click();


  });


});

Answer №1

To check for multiple classes, simply use dots between them:

by.css('.btn.btn-lg.btn-primary.btn-block')

However, I recommend finding the button by its text as it is more reliable and readable:

by.xpath('//button[. = "login"]')

If possible, consider assigning an id attribute to the button to make testing easier:

<button id="submitButton" class="btn btn-lg btn-primary btn-block" type="submit" ng-disabled="loading">login</button>

Then, locating the button would be as simple as:

by.id('submitButton')

Answer №2

Utilizing a single class for locating the element is another viable option

by.css('.btn-primary')

based on the class name

by.className('btn-primary')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Google analytics page tracking feature is experiencing issues within an AngularJS application and is not functioning correctly

My current project focuses on AngularJS, catering to both mobile applications and desktop websites. I have downloaded analytics.js locally and integrated it into my GA tracking code within the index.html file: (function(i,s,o,g,r,a,m){i['GoogleAnalyt ...

Creating a directive for encapsulating angular typeAhead functionality

My aim was to package the angular-ui typeAhead directive into a reusable component, allowing for usage like this: <input my-person-lookup="myModel.personId" ></input> I accomplished this by creating a custom directive and setting a static tem ...

A recursive approach for constructing a tree structure in Angular

Currently, I am working on a project involving the implementation of crud functions. To display the data in a tree-like structure, I am utilizing the org chart component from the PrimeNg library. The data obtained from the backend is in the form of an arra ...

When I click the back button on my mouse in React, it returns JSON data instead of HTML and CSS

I am working on a web application with two pages: a menu and orders. When I navigate from the orders page to the menu page and click the back button, I receive JSON data instead of the HTML page. The data loads correctly, but the page does not display the ...

Is there a way to capture a word from a webpage with just a double click as an input?

I am interested in creating a web application that can take input from the user by double-clicking on a word within the webpage. I have seen this feature utilized in the E2B dictionary Google Chrome extension and would like to implement something similar. ...

Increase division height when mouse hovers over it

I want the height of the div to be 50px by default and change to 300px onmouseover. I have implemented this as follows: <style type="text/css"> #div1{ height:50px; overflow:hidden; } #div1:hover{ height:300px; } </style> <body> <div i ...

Using Regular Expression in JavaScript to replace variables within a string

Looking for assistance to replace keywords in a string with pre-defined variables. Currently, the code only displays variable names instead of their content. Can anyone provide help? Desired Output: 1111Test1 2222Test2 3333Test3 Current Output ...

Building a Laravel PHP application that dynamically generates a custom JSON object fetched from the database and passes it from PHP to

I am faced with the task of generating a custom JSON object by organizing data retrieved from PHP in my Controller. I have full control over what information goes where and in what specific order. To accomplish this, it seems like I will need to go throug ...

What steps should I take to troubleshoot the issue when utilizing full HTML link paths in my links?

I have come across the recommendation to use full link paths between pages on my website for various reasons. However, I am concerned about how to debug and work on my local testing environment when all of the links are using full paths. (Manually replaci ...

Using jQuery to apply a class based on JSON data

This file contains JSON data with details about seat information. var jsonData = { "who": "RSNO", "what": "An American Festival", "when": "2013-02-08 19:30", "where": "User Hall - Main Auditorium", "seats": ["0000000000000000001111111 ...

Trouble Strikes: Heroku Deployment Fails for Node.js/React App

Currently, I am in the process of working on a React/Node application using create-react-app and attempting to deploy it through Heroku. However, we are encountering an error message related to react-scripts when deploying on Heroku: Error: sh: 1: react-sc ...

What is the reason behind the continual change in the background image on this website?

Can you explain the functionality of this background image? You can find the website here: ...

The compilation of create-react-app has encountered an error, however the process will continue running

I'm encountering a peculiar issue with my application built using create-react-app. It seems that the script refuses to exit when an error occurs. For example: ~/my-project$ ./node_modules/.bin/react-scripts build Creating an optimized production buil ...

Unable to trigger jQuery onclick event on nested div elements

I'm experiencing an issue with my web-page where two buttons at the top are not responding to a sorting function on the nested #byFilter. Despite trying to apply the onclick(function()) method, it doesn't seem to work. Javascript $(document).re ...

Google Chrome successfully transmits two variables with AJAX, whereas Mozilla Firefox does not send them

Having an issue with sending two values through ajax - currently only one value is being sent in Mozilla browser. Ajax script: $('#post_submit').click(function() { event.preventDefault(); var great_id = $("#post_container_supreme:first").attr(" ...

Change the local date and time to UTC in the format of yy:mm:dd H:M

I must change the date format from local time to UTC or ISO as yy:mm:dd H:M, or calculate the difference between local date times with 03:30 as yy:mm:dd H:M 2016-10-22T04:30:00.000Z convert this to 2016-10-22T01:00:00.000Z ...

What is the most efficient way to establish a connection to "host:port" without relying on a web browser, utilizing only Node.js/JavaScript?

To establish a connection with a server (created using node js), we simply open the browser and include some code in a file like this: <script src="http://127.0.0.1:8080/socket.io/socket.io.js"></script> <script type="text/javascript">/* ...

inserting information into HTML document

I've noticed that this particular method hasn't been addressed yet due to the document.GetElementsByClassName I'm using, but for some reason it's not working. My goal is to add a specific value (a string) to the page. I have located an ...

What sets my project apart from the rest that makes TypeScript definition files unnecessary?

Utilizing .js libraries in my .ts project works seamlessly, with no issues arising. I have not utilized any *.d.ts files in my project at all. Could someone please explain how this functionality is achievable? ...

Creating a webpage with a left panel and draggable elements using JavaScript/jQuery

As someone new to Javascript/jQuery, I have been given the task of creating a web page with elements in a "pane" on the left side that can be dragged into a "droppable" div area on the right side. The entire right side will act as a droppable zone. I am u ...