Code Components
Re-using code is a very useful working method, saves time and energy when it comes to performing the same actions or any common action on each test, therefore when it needs an update or a fix, you only need to fix it in the script file itself.
Scripts can call the functions in the file using the po object statement.
Functions file:
1
module.exports = {
2
Login: function(username, password) {
3
web.type('id=userName',username);
4
web.type('id=password',password);
5
web.click('name=login');
6
web.assertText('id=firstHeading', 'Welcome ' + username);
7
}
8
}
Copied!
it's also possible to use the ES6 arrow function and template literals
1
module.exports = {
2
Login: (username, password) => {
3
web.type('id=userName', username);
4
web.type('id=password', password);
5
web.click('name=login');
6
web.assertText('id=firstHeading', `Welcome ${username}`);
7
}
8
}
Copied!
Script to run:
1
web.transaction('Opening browser and web-page');
2
web.init();
3
web.open('https://oxygenhq.org');
4
web.transaction('Logging in');
5
6
po.Login('User1', 'Password123');
7
8
web.click('id=button1');
Copied!
In the above example , the script shall use the code it has inherited from a file called oxygen.po.js, it can contain many functions as you want and call it from any script you are running.
Last modified 5mo ago
Copy link