Skip to content

Commit e95f812

Browse files
committed
form api complete code
1 parent 1a2b3ab commit e95f812

File tree

5 files changed

+135
-56
lines changed

5 files changed

+135
-56
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"window": "env-cmd wdio run ./wdio.conf.ts --cucumberOpts.tagExpression='@window'",
3131
"dragdrop": "env-cmd wdio run ./wdio.conf.ts --cucumberOpts.tagExpression='@dragdrop'",
3232
"slider": "env-cmd wdio run ./wdio.conf.ts --cucumberOpts.tagExpression='@slider'",
33+
"form": "env-cmd wdio run ./wdio.conf.ts --cucumberOpts.tagExpression='@form'",
3334
"sampletag": "env-cmd wdio run ./wdio.conf.ts --cucumberOpts.tagExpression='@sampletag'",
3435
"test-typescript": "env-cmd npx ts-node typescript-test.ts"
3536
}

wdio-test/features/form-api-features/form-api.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Feature: Letcode Form Page
22

33
@regression @form
4-
Scenario Outline: As a user, I can select an option using visible text
4+
Scenario Outline: As a user, I can complete the form using API data
55

66
Given I am on letcode workspace page
77
When I click form page

wdio-test/page-objects/form-api-pages/form-api.page.ts

Lines changed: 115 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,137 @@
11
import Page from '../page.js';
2-
import apiHelper from '../../helper/api-helper';
2+
import apiHelper from '../../../helper/api-helper.js';
33

44
class FormPage extends Page {
55

66
get openHomeButton(){ return $(`//button[normalize-space()='Open Home Page']`) };
77

88

9-
async clickOpenHomeButton(){
10-
const elmOpenHomeButton = await this.openHomeButton;
11-
await this.click(elmOpenHomeButton);
12-
}
9+
1310

14-
RANDOM_USER_API_URL="https://random-data-api.com"
15-
RANDOM_USER_API_ENDPOINT="/api/users/random_user"
1611
userInfo = apiHelper.GET(process.env.RANDOM_USER_API_URL, process.env.RANDOM_USER_API_ENDPOINT, '', {})
17-
friendlyName = ''
1812
firstName = ''
1913
lastName = ''
20-
birthDate = ''
21-
gender = ''
22-
maritalStatus = ''
23-
phoneCountryCode = ''
24-
phoneNumber = ''
2514
email = ''
15+
phoneCountry = ''
16+
phoneNumber = ''
2617
addressLine1 = ''
27-
country = ''
28-
province = ''
29-
city = ''
18+
addressLine2 = ''
19+
state = ''
3020
postalCode = ''
31-
workAvailability = ''
32-
currentSalaryCurrency = ''
33-
currentSalary = ''
34-
expectedSalaryCurrency = ''
35-
expectedSalary = ''
21+
country = ''
22+
birthDate = ''
23+
gender = ''
3624

3725
async initializeRandomData() {
3826
const apiRawResult = await this.userInfo;
3927
const apiResult = apiRawResult.body;
40-
this.friendlyName = 'Auto' + apiResult.first_name + this.randomString(5);
41-
this.firstName = apiResult.first_name
42-
this.lastName = 'Automation'
43-
this.birthDate = apiResult.date_of_birth
44-
this.gender = 'Male'
45-
this.maritalStatus = 'Single'
46-
this.phoneCountryCode = '+63'
47-
this.phoneNumber = '' + await this.getRandomInt(9000000000, 9999999999)
28+
//for dates we can use moment.js but for this case we will use browser keys so random int is used
29+
const randomMonth = await this.getRandomInt(1, 11);
30+
const randomDay = await this.getRandomInt(1, 27);
31+
const randomYear = await this.getRandomInt(1960, 2010);
32+
33+
this.firstName = apiResult.first_name;
34+
this.lastName = apiResult.last_name;
35+
this.birthDate = ''+randomMonth+randomDay+randomYear;
36+
this.gender = 'Male';
37+
this.phoneCountry = 'PH';
38+
this.phoneNumber = '' + await this.getRandomInt(9000000000, 9999999999);
4839
this.email = apiResult.username + await this.getRandomInt(1000, 9999) + '@email.com';
49-
this.addressLine1 = apiResult.address.street_address
50-
this.country = 'Philippines'
51-
this.province = 'Pampanga'
52-
this.city = 'Angeles City'
53-
this.postalCode = '1234'
54-
this.workAvailability = 'Immediate'
55-
this.currentSalaryCurrency = 'PHP'
56-
this.currentSalary = '30000'
57-
this.expectedSalaryCurrency = 'PHP'
58-
this.expectedSalary = '60000'
40+
this.addressLine1 = apiResult.address.street_address;
41+
this.addressLine2 = apiResult.address.street_name;
42+
this.country = apiResult.country;
43+
this.state = apiResult.address.state;
44+
this.postalCode = '' + await this.getRandomInt(1000, 9999);
45+
}
46+
47+
48+
get firstNameInput(){ return $(`//input[@id='firstname']`) };
49+
get lastNameInput(){ return $(`//input[@id='lasttname']`) };
50+
get emailInput(){ return $(`//input[@id='email']`) };
51+
get countryCodeSelect(){ return $(`//div[@class='select']//select`) };
52+
get phoneNumberInput(){ return $(`//input[@id='Phno']`) };
53+
get addressLine1Input(){ return $(`//input[@id='Addl1']`) };
54+
get addressLine2Input(){ return $(`//input[@id='Addl2']`) };
55+
get stateInput(){ return $(`//input[@id='state']`) };
56+
get postalCodeInput(){ return $(`//input[@id='postalcode']`) };
57+
get countrySelect(){ return $(`//label[normalize-space()='Country']//following-sibling::div//select`) };
58+
get dateOfBirthInput(){ return $(`//input[@id='Date']`) };
59+
get genderRadio(){ return $(`//input[@id='male']`) };
60+
get checkTerms(){ return $(`//input[@type='checkbox']`) };
61+
get submitButton(){ return $(`//input[@type='submit']`) };
62+
63+
async inputFirstName(){
64+
const elmFirstNameInput = await this.firstNameInput;
65+
await this.inputText(elmFirstNameInput,this.firstName);
66+
}
67+
68+
async inputLastName(){
69+
const elmLastNameInput = await this.lastNameInput;
70+
await this.inputText(elmLastNameInput,this.lastName);
71+
}
72+
73+
async inputEmail(){
74+
const elmEmailInput = await this.emailInput;
75+
await elmEmailInput.clearValue();
76+
await this.inputText(elmEmailInput,this.email);
77+
}
78+
79+
async selectCountryCode(){
80+
const elmCountryCodeSelect = await this.countryCodeSelect;
81+
await this.click(elmCountryCodeSelect);
82+
await browser.keys(this.phoneCountry);
83+
await browser.keys('Enter')
84+
}
85+
86+
async inputPhoneNumber(){
87+
const elmPhoneNumberInput = await this.phoneNumberInput;
88+
await this.inputText(elmPhoneNumberInput,this.phoneNumber);
89+
}
90+
91+
async inputAddressLine1(){
92+
const elmAddressLine1Input = await this.addressLine1Input;
93+
await this.inputText(elmAddressLine1Input,this.addressLine1);
94+
}
95+
96+
async inputAddressLine2(){
97+
const elmAddressLine2Input = await this.addressLine2Input;
98+
await this.inputText(elmAddressLine2Input,this.addressLine2);
99+
}
100+
101+
async inputState(){
102+
const elmStateInput = await this.stateInput;
103+
await this.inputText(elmStateInput,this.state);
104+
}
105+
106+
async inputPostalCode(){
107+
const elmPostalCodeInput = await this.postalCodeInput;
108+
await this.inputText(elmPostalCodeInput,this.postalCode);
109+
}
110+
111+
async selectCountry(){
112+
const elmCountrySelect = await this.countrySelect;
113+
await elmCountrySelect.selectByVisibleText('Philippines');
114+
}
115+
116+
async inputDateOfBirth(){
117+
const elmDateOfBirthInput = await this.dateOfBirthInput;
118+
await this.click(elmDateOfBirthInput);
119+
await browser.keys(this.birthDate);
120+
}
121+
122+
async selectGender(){
123+
const elmGenderRadio = await this.genderRadio;
124+
await this.click(elmGenderRadio);
125+
}
126+
127+
async selectTermsAndConditions(){
128+
const elmcheckTerms = await this.checkTerms;
129+
await this.click(elmcheckTerms);
130+
}
131+
132+
async clickSubmit(){
133+
const elmSubmitButton = await this.submitButton;
134+
await this.click(elmSubmitButton);
59135
}
60136

61137
}

wdio-test/page-objects/page.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export default class Page {
8484
async getRandomInt(min, max) {
8585
min = Math.ceil(min);
8686
max = Math.floor(max);
87-
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive. ex. random 1-3 is 1 min, max 4
87+
//The maximum is exclusive and the minimum is inclusive. ex. random 1-3 is 1 min, max 4
88+
return Math.floor(Math.random() * (max - min) + min);
8889
}
8990
}

wdio-test/step-definitions/form-api-steps/form-api.step.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,61 @@
11
import { Given, When, Then } from '@wdio/cucumber-framework';
2-
2+
import FormApiPage from '../../page-objects/form-api-pages/form-api.page.js';
33

44
When(/^I input form first name$/, async () => {
5-
return true;
5+
await FormApiPage.initializeRandomData();
6+
await FormApiPage.inputFirstName();
67
});
78

89
When(/^I input form last name$/, async () => {
9-
return true;
10+
await FormApiPage.inputLastName();
1011
});
1112

1213
When(/^I input form email$/, async () => {
13-
return true;
14+
await FormApiPage.inputEmail();
1415
});
1516

1617
When(/^I select form country code$/, async () => {
17-
return true;
18+
await FormApiPage.selectCountryCode();
1819
});
1920

2021
When(/^I input form phone number$/, async () => {
21-
return true;
22+
await FormApiPage.inputPhoneNumber();
2223
});
2324

2425
When(/^I input form address line 1$/, async () => {
25-
return true;
26+
await FormApiPage.inputAddressLine1();
2627
});
2728

2829
When(/^I input form address line 2$/, async () => {
29-
return true;
30+
await FormApiPage.inputAddressLine2();
3031
});
3132

3233
When(/^I input form state$/, async () => {
33-
return true;
34+
await FormApiPage.inputState();
3435
});
3536

3637
When(/^I input form postal code$/, async () => {
37-
return true;
38+
await FormApiPage.inputPostalCode();
3839
});
3940

4041
When(/^I select form country$/, async () => {
41-
return true;
42+
await FormApiPage.selectCountry();
4243
});
4344

4445
When(/^I input form date of birth$/, async () => {
45-
return true;
46+
await FormApiPage.inputDateOfBirth();
4647
});
4748

4849
When(/^I select form gender$/, async () => {
49-
return true;
50+
await FormApiPage.selectGender();
5051
});
5152

5253
When(/^I click agree to form terms and condition$/, async () => {
53-
return true;
54+
await FormApiPage.selectTermsAndConditions();
5455
});
5556

5657
When(/^I click form submit button$/, async () => {
57-
return true;
58+
await FormApiPage.clickSubmit();
5859
});
5960

6061
Then(/^I should be able to verify that the form is successfully submitted$/, async () => {

0 commit comments

Comments
 (0)