Search code examples
typescriptappiumreturn-valuewebdriver-ioappium-android

WebdriverIo/ Typescript - How to call stored variables?


I'm using WebdriverIO with Typescript to automate an Adnroid app. Scenario:

  1. Navigate to Training Page
  2. Retrieve Session name (This value dynamically changes)
  • So I want to store the retrieved session name in a varaible and assert it later

This is what I have done. But it's not working. So can someone pointout to me what I have done wrong here?

Thanks!!

training.ts

        import BaseAppScreen from "./base-app.screen";
    
    
    const SELECTORS = {
        Training_Card_Session_Name: (`~trainingItemTitle0`), // This is the one dynamically changing
    };
    
    export default class TrainingScreen extends BaseAppScreen{
        constructor() {
            super(); 
                $(SELECTORS.Training_Card_Session_Name).waitForDisplayed();
        }
    
        getSessionNameOfTrainingCard(): string{ 
            var trainingSessionName = $(SELECTORS.Training_Card_Session_Name).getText();
return trainingSessionName; // Here I want to retrive the getText value and store it in a variable to be called later.
        }
    
        
    }

training.spec.ts

    import TrainingScreen from '../screenobjects/training.screen';
    
    describe('Training Sessions', () => {
      let trainingScreen: TrainingScreen;

let trainingSessionName: string;
    
    
      beforeAll(() => {
        loginScreen = new LoginScreen();   
      });
    
      
      it(`Select a training Card`, () => {
        trainingScreen.getSessionNameOfTrainingCard();
        expect(trainingScreen.getSessionHeaderTitle()).toEqual(trainingSessionName);; // this is where I need to assert the previously stored text
      });
    
    });

Solution

  • What I have done above is correct.

    In training.spec.ts, no need to call the varaible inside toEqual assertion. trainingScreen.getSessionHeaderTitle(), it self stores the value of returned text inside trainingSessionName variable. Therefore, can assert it to be equal to the actual title name.

    i.e.

    expect(trainingScreen.getSessionHeaderTitle()).toEqual('TRAINING'); // The title TRAINING is a static value.