CodeceptJs - Getting List of elements and checking attributes of elements

codeceptjs examples
codeceptjs run-multiple
codeceptjs visual studio code
codeceptjs scenario todo
codeceptjs bdd
codeceptjs timeout
javascript acceptance testing
concept js

I'm QA Automation with 7+ years of experiance in C#+Nunit+Webdriver. Now I have to switch to CodeceptJS and can't find how to do some common actions using this framework. Please, help me with next questions:

  1. How can I get list of elements and get some attribute of specific element in this list. In C# it will be something like this:

    var list = driver.FindElements(By.Xpath("*//xpath"));
    
  2. How to check if element has some attribute in customsteps or pageobject?

    module.exports = {
     MyElemThatIsNotAlwaysVisible_Likesubmenu: { id: "submenu" },
     MyElementThatIsAlwaysVisible_Likemuenu : {id: "menu"}
    
     function_click_onelement_that_notalwaysvisible () 
     {
       //here I want to check 
       if(MyElementThatIsAlwaysVisible.getattribute("class").contains("not-active"))
       {
          MyElementThatIsAlwaysVisible_Likemuenu.click();
          wait.until (MyElemThatIsNotAlwaysVisible_Likesubmenu.Displayed)
       }
       MyElemThatIsNotAlwaysVisible_Likesubmenu.Click();
     }
    }
    

  1. If you want to get and do something more with the list, you can use:

await I.grabAttributeFrom('//img', 'src');

If xPath matches more than one element, method returns an array. Documentation reference is here.


  1. If you need to validate only that the attribute exists, you can use:

    I.seeAttributesOnElements('//form', { method: "post"});

as stated in here in documentation.

Locators, CodeceptJS provides flexible strategies for locating elements: XPath locators; Semantic locators: by link text, by button text, by field names, etc. Custom Locator Strategies: by data attributes or whatever you prefer. If nothing found, check if locator looks like a CSS selector. Get element at position:. CodeceptJS bypasses execution commands to helpers. Depending on the helper enabled, your tests will be executed differently. If you need cross-browser support you should choose Selenium-based WebDriver or TestCafé. If you are interested in speed - you should use Chrome-based Puppeteer. The following is a diagram of the CodeceptJS architecture:


Found answers:

1) Add CustomHelper and inside it add functions

async GetListOfElements(locator) {
    let browser = this.helpers.WebDriver;
    let res = browser._locateFields(locator);
    return res;
  };

this will return list of elements Later if you need to do something with specific item can add one more helper function like

  async Clicker(elem) {
    let browser = this.helpers.WebDriver.browser;
    await browser.elementClick(elem.elementId);
  };

In this way you can use any methods from WebdriverIo API 2) You can add async function and in it something like

  var attr = await I.grabAttributeFrom(this.MyElement, "class");
    if (attr.toString().includes("checked"))
      myaction();

Acceptance Testing, By opening the browser, accessing a site, clicking on links, filling in the locate element by [aria-label] attribute in web // or by accessibility id in  CodeceptJS allows to create custom locator strategies and use them in tests. This way you can define your own handling of elements using specially prepared attributes of elements. What if you use special test attributes for locators such as data-qa, data-test, test-id, etc. We created customLocator plugin to declare rules for locating element.


I made a custom helper using protractor that does this since the getattrib method in codeceptjs never worked for me either, you can edit the "textcontent" value to get different attributes.

  /**
   * Function to return the text content of all elements matching with the locator in an array
   * @param xpath object
   */
  async getElementsText(locator) {
    const driver = this.helpers.Protractor.browser;
    await driver.waitForAngular();
    // await console.log("Getting text for: " + locator.xpath);
    return driver.element.all(by.xpath(locator.xpath)).getAttribute("textContent");
  }

Getting Started, CodeceptJS is a modern end to end testing framework with a special BDD-style syntax. To list all available commands for the current configuration run locate element by [aria-label] attribute in web // or by accessibility id in  Stack Overflow Public questions and answers; CodeceptJs - Getting List of elements and checking attributes of elements. 2.


WebDriver, installed via NPM. Please check Testing with WebDriver for more details. Get elements by different locator types, including strict locator. Should be used in  Elements can also be located by their accessability id, available both at Android and iOS. Accessibility id is recommended to use for locating element, as it rarely changed. iOS uses UIAccessibilityIdentification; Android accessibility id matches the content-description; Web view uses [aria-label] attribute as accessibility id


Protractor, Protractor helper is based on Protractor library and used for testing web _startBrowser(); capabilities : {} - list of Desired Capabilities; proxy Get elements by different locator types, including strict locator Should be Retrieves an attribute from an element located by CSS or XPath and returns it to test. Supercharged End 2 End Testing Framework for NodeJS - Codeception/CodeceptJS


CodeceptJS, Access From Helpers. Receive a Appium client from a custom helper by accessing browser property: Get list of all available contexts. let contexts = await Checks that all elements with given locator have given attributes. Introduction. This article is a great place to start if you’re interested in working with list attributes in FME. Consider this your gateway where you’ll find introductory information as well as links to FME List Attribute documentation, example tutorials with workspace templates, and other FME resources related to List Attributes and data manipulation.