Skip to content

Commit dc4e649

Browse files
comments added for tests
1 parent 05a7ab9 commit dc4e649

File tree

6 files changed

+48
-11
lines changed

6 files changed

+48
-11
lines changed

libraries/logger.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def self.fail(message)
108108
end
109109
end
110110

111+
# At the end of all the test run, the console logs will be written to a new log file under reports/logs which can be used for future reference
111112
at_exit {
112113
File.open("reports/logs/[" + Time.now.localtime.to_s + "].log", "w+") do |f|
113114
f.puts($logger_text_array)

locators/profile_preview.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def initialize
2626
@country = Locator.new(:css, "section.up-card-section span[itemprop='country-name']")
2727
@earned = Locator.new(:xpath, "//section[contains(@class,'up-card-section')]//div[contains(@class, 'col-compact')][1]")
2828
@overview_expand_button = Locator.new(:css, "div.up-line-clamp button[aria-expanded='false']")
29-
@overview = Locator.new(:css, "section.row div.up-line-clamp-expanded")
29+
@overview = Locator.new(:css, "section.row section.up-card-section div.up-line-clamp-expanded")
3030
@upskill_tags = Locator.new(:css, "section.row section.up-card-section div.skills span")
3131
@associated_with = Locator.new(:css, "section.row div.mt-30 div.d-flex")
3232

pages/profile_preview.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def create_hash_for_client_details
3737

3838
# To return if the profile preview page is displayed
3939
def is_profile_preview_displayed?
40-
return @overview.is_present_with_wait?(30)
40+
return @upskill_tags.is_present_with_wait?(30)
4141
end
4242

4343
end

spec/spec_helper.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# This is to implement the rspec's test flow structure that is used to define the tests
2+
# This is implemented using Nested Layouts concept
3+
# This is responsible for the logging of description, test steps and the numbering are happening here
4+
# Before all, before each, after all, after each steps are also declared here
5+
# This file can be taken as a backbone for the tests that are going to be scripted in this framework
16
require_relative "../libraries/logger.rb"
27
require_relative "../libraries/driver.rb"
38
require_relative "../libraries/config.rb"
@@ -9,30 +14,37 @@
914
$test = 0
1015
$step = 0
1116

17+
# Before all block which can be declared for each test file
1218
def before_all(&block)
1319
yield if block_given?
1420
end
1521

22+
# After all block which can be declared for each test file
1623
def after_all(&block)
1724
$after_all = lambda { yield }
1825
end
1926

27+
# This will be executed before each `it` described in the tests
2028
def before_each_test(&block)
2129
$before_each_test = lambda { yield }
2230
end
2331

32+
# This will be executed after each `it` described in the tests
2433
def after_each_test(&block)
2534
$after_each_test = lambda { yield }
2635
end
2736

37+
# This will be executed before each `step` described in the tests
2838
def before_each_step(&block)
2939
$before_each_step = lambda { yield }
3040
end
3141

42+
# This will be executed after each `step` described in the tests
3243
def after_each_step(&block)
3344
$after_each_step = lambda { yield }
3445
end
3546

47+
# This is to log the description and reset the counter for the tests and steps
3648
def describe(description)
3749
$file = $file + 1
3850
$test = 0
@@ -42,6 +54,7 @@ def describe(description)
4254
$after_all.call if $after_all
4355
end
4456

57+
# This is to log the steps declared in the tests
4558
def step(description)
4659
$step = $step + 1
4760
$before_each_step.call if $before_each_step
@@ -50,6 +63,7 @@ def step(description)
5063
$after_each_step.call if $after_each_step
5164
end
5265

66+
# This is to log the `it` declared in the tests
5367
def it(description)
5468
$test = $test + 1
5569
$before_each_test.call if $before_each_test

spec/test_find_professionals.rb

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# This is the actual test file
2+
# Here I used Page Object Model to declare the locators and use within the tests
3+
# This test skeleton structure is built to look similar with rspec test structure
14
require_relative 'spec_helper.rb'
25
require_relative '../pages/home.rb'
36
require_relative '../pages/search_listing.rb'
@@ -6,8 +9,10 @@
69
require 'yaml'
710
include Pages
811

12+
# Each test file should have one describe block with the text that states the higher level module which is going to be tested
913
describe("Find Professionals in Upwork") do
1014

15+
# Before all block where we need initialize all the pages and load the test data that is going to be used across this file
1116
before_all do
1217
@@find_prof_page = Home.new
1318
@@search_listing = SearchListing.new
@@ -16,12 +21,21 @@
1621
@@test_data = YAML.load_file(Dir.pwd + '/test-data/find_professionals.yml')
1722
end
1823

24+
# After all Make sure the browser driver initialized is quit properly
1925
after_all do
2026
@@driver.quit
2127
end
2228

29+
# This is added in additional to the rspec's structure
30+
# Here you can specify multiple search_key in the test_data yaml file in array format
31+
# This test will run iteratively for all the search key's and log the results
2332
@@test_data['search_keyword'].each do |search_key|
33+
# This is where the actual test starts
34+
# A file can have multiple `it` statements within a `describe` block
2435
it("Find professionals using search option in Upwork") do
36+
# Each step defines an important action that is made in the browser
37+
# Each step will be logged to the console,
38+
# So its better to have only the important test steps that has to be noted by manual person here
2539
step("Run " + Config.browser + " brower with headless as " + Config.headless.to_s) do
2640
@@driver = Driver.new
2741
end
@@ -31,7 +45,7 @@
3145
step("Navigate to Upwork website - " + Config.url) do
3246
@@driver.get(Config.url)
3347
if @@captcha.captcha.is_present_with_wait?(10)
34-
sleep(60)
48+
sleep(60) # To handle the captcha page
3549
@@captcha.handle_captcha_page
3650
end
3751
expect_to_equal(@@find_prof_page.find_professionals_and_agencies_text_box.is_present_with_wait?(30), true, "Home page is displayed")
@@ -48,14 +62,18 @@
4862
@@driver.clear_cookies
4963
@@driver.get(@@test_data["url_with_search_key"] + search_key)
5064
end
51-
sleep(60) if @@captcha.captcha.is_present_with_wait?(10)
65+
sleep(60) if @@captcha.captcha.is_present_with_wait?(10) # To handle the captcha page
5266
expect_to_equal(@@search_listing.is_search_listing_displayed?, true, "Search listing page is displayed")
5367
end
5468
step("Parsing the first page with search results and storing the results in hash of hash format") do
5569
@@result_hash = @@search_listing.parse_results_page_and_store_values
56-
Log.info("Search results parsed data -> #{@@result_hash}")
70+
Log.info("Search results parsed data -> #{@@result_hash}") # Log.info methods wont be printed in the stdout, one can change the logger level to get these
5771
end
5872
step("Validate the results hash along with the search key that is used to get the results") do
73+
# Each freelancer's detail is matched with the search key
74+
# And if the key word is not matched with any of the attributes, fail log will be printed
75+
# Else pass log will be prited for that freelancer with the name
76+
# log.info methods will give indepth comparison, if needed one can change the logger level
5977
@@result_hash.each do |key, value|
6078
pass_result = {}
6179
fail_result = {}
@@ -88,9 +106,9 @@
88106
end
89107
end
90108
step("Click on the random profile from the search result and fetch the freelancer details from the freelancers preview page") do
91-
@@profile_count = rand(1..10)
109+
@@profile_count = rand(1..10) # clicking on random profile from the list dispalyed
92110
@@search_listing.click_on_freelancer_profile(@@profile_count)
93-
if !@@profile_preview.is_profile_preview_displayed?
111+
if !@@profile_preview.is_profile_preview_displayed? # To handle the captcha page
94112
@@driver.quit
95113
@@driver = Driver.new
96114
@@driver.clear_cookies
@@ -102,14 +120,17 @@
102120
step("Fetch details from the freelancers profile preview and compare it with the details dispalyed in search results page") do
103121
preview_hash = @@profile_preview.create_hash_for_client_details
104122
Log.info("Freelancer profile fetched details -> #{preview_hash}")
123+
# Following are the assertions for the details fetched from the search list page and the freelancer profile preview page
124+
# Each attributes are having different format in the UI, hence while fetching each one will have different structure
125+
# Some of them are modifed to make it compatible for comparing
105126
expect_to_equal(@@result_hash[@@profile_count][:name], preview_hash[:name], "Profile preview and search listing details match for name with values #{@@result_hash[@@profile_count][:name]} == #{preview_hash[:name]}")
106127
expect_to_equal(@@result_hash[@@profile_count][:title], preview_hash[:title], "Profile preview and search listing details match for title with values #{@@result_hash[@@profile_count][:title]} == #{preview_hash[:title]}")
107128
expect_to_equal(@@result_hash[@@profile_count][:country], preview_hash[:country], "Profile preview and search listing details match for country with values #{@@result_hash[@@profile_count][:country]} == #{preview_hash[:country]}")
108-
expect_to_equal(preview_hash[:earned]&.split(" ")[0], @@result_hash[@@profile_count][:earned]&.split("\n")[0], "Profile preview and search listing details match for earned with values #{@@result_hash[@@profile_count][:earned]} == #{preview_hash[:earned]&.delete("\n")}") if @@result_hash[@@profile_count][:earned]
109-
expect_to_equal(@@result_hash[@@profile_count][:overview]&.delete("\n")&.delete(' '), preview_hash[:overview]&.delete("\n")&.delete(' '), "Profile preview and search listing details match for overview with values #{@@result_hash[@@profile_count][:overview]&.delete("\n")} == #{preview_hash[:overview]&.delete("\n")}")
129+
expect_to_equal(preview_hash[:earned]&.split(" ")[0], @@result_hash[@@profile_count][:earned]&.split("\n")[0].split(" ")[0], "Profile preview and search listing details match for earned with values #{@@result_hash[@@profile_count][:earned]} == #{preview_hash[:earned]&.delete("\n")}") if @@result_hash[@@profile_count][:earned]
130+
expect_to_equal(@@result_hash[@@profile_count][:overview]&.delete("\n")&.delete(' '), preview_hash[:overview]&.delete("\n")&.delete(' ')&.split("lessShowlesstext")[0], "Profile preview and search listing details match for overview with values #{@@result_hash[@@profile_count][:overview]&.delete("\n")} == #{preview_hash[:overview]&.delete("\n")}")
110131
expect_to_equal(@@result_hash[@@profile_count][:upskill_tags], preview_hash[:upskill_tags], "Profile preview and search listing details match for upskill_tags with values #{@@result_hash[@@profile_count][:upskill_tags]} == #{preview_hash[:upskill_tags]}")
111132
expect_to_equal(@@result_hash[@@profile_count][:price]&.delete(' '), preview_hash[:price]&.delete(' '), "Profile preview and search listing details match for price with values #{@@result_hash[@@profile_count][:price]&.delete(' ')} == #{preview_hash[:price]&.delete(' ')}")
112-
expect_to_equal((preview_hash[:success_rate]&.delete("\n")&.delete(' ')&.include? @@result_hash[@@profile_count][:success_rate]&.delete(' ')), true, "Profile preview and search listing details match for success_rate with values #{preview_hash[:success_rate]&.delete("\n")} == #{@@result_hash[@@profile_count][:success_rate]&.delete(' ')}")
133+
expect_to_equal((preview_hash[:success_rate]&.delete("\n")&.delete(' ')&.include? @@result_hash[@@profile_count][:success_rate]&.delete(' ')), true, "Profile preview and search listing details match for success_rate with values #{preview_hash[:success_rate]&.delete("\n").to_s} == #{@@result_hash[@@profile_count][:success_rate]&.delete(' ').to_s}")
113134
expect_to_equal(@@result_hash[@@profile_count][:associated_with]&.split("\n")[1], preview_hash[:associated_with]&.split("\n")[0], "Profile preview and search listing details match for associated_with with values #{@@result_hash[@@profile_count][:associated_with]&.split("\n")[1]} == #{preview_hash[:associated_with]&.split("\n")[0]}") if @@result_hash[@@profile_count][:associated_with]
114135
end
115136
end

test-data/find_professionals.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
search_keyword:
1+
# Test specific data are declared here
2+
search_keyword: # On giving multiple key word, same test will be running with different search keys. Duplication of code can be avoided here
23
- Automation
34
url_with_search_key: https://www.upwork.com/search/profiles/?nbs=1&q=
45
url_with_profile_preview: https://www.upwork.com/search/profiles/?nbs=1&q=Automation&profile=~01dabbff50ebecbf93

0 commit comments

Comments
 (0)