Skip to content

Commit 8f08b12

Browse files
authored
Merge pull request #26 from the-mentor/master
Added Examples and Impoved starting chrome in headless mode.
2 parents fb3aa45 + 7cb5885 commit 8f08b12

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

Examples/A-Simple-Google-Search.ps1

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<#
2+
.VERSION - 0.2
3+
4+
.DESCRIPTION
5+
This is an example script that will show you how to use the Selenium driver to preform a simple google search.
6+
Using this example script you will learn the basic usage of the Selenium powershell module.
7+
Each comment line will explain the line that will come after it so you can follow it in a step by step fashion.
8+
#>
9+
10+
# The line below will Import-Module Selenium if it fails it will display the installation command and stop the script.
11+
try{Import-Module -Name Selenium -ErrorAction Stop}catch{Write-Host 'Importing the Selenium module failed. Please install it using the following command: Install-Module Selenium';break}
12+
13+
# Start the Selenium Chrome Driver
14+
$Driver = Start-SeChrome
15+
16+
# Next we will check if the driver is running and if it's not running we will show a message. If the driver is running we will run the commands inside the if statment.
17+
if($Driver){
18+
# Now that we verified that the driver is running we can start doing cool things with it.
19+
20+
# Using the Enter-SeUrl command we can tell the driver to go to any URL we want in this case we'll go to https://google.com/ncr
21+
# I used the /ncr in the end of the URL because I want google to always stay in english and not redirect to a specific language for consistency.
22+
Enter-SeUrl -Driver $Driver -Url 'https://www.google.com/ncr'
23+
24+
# After nevigating to the desired URL we can start interacting with elements on the page in this example we are going to find the search box and type something in it.
25+
# We can find elements using different ways like the element id or the Name/ClassName/TagName and a few other ways.
26+
# In the below code we're going to show you a few ways to solve this problem.
27+
28+
<# This is the HTML code of the search box
29+
<input class="gLFyf gsfi" maxlength="2048" name="q" type="text" jsaction="paste:puy29d" aria-autocomplete="both" aria-haspopup="false" autocapitalize="off" autocomplete="off" autocorrect="off" role="combobox" spellcheck="false" title="Search" value="" aria-label="Search">
30+
#>
31+
32+
# By examining the HTML code of the google seach box we can see that the search box name is q so in the below example we'll use the name q to find its element
33+
$SearchBoxElement = Find-SeElement -Driver $Driver -Name q
34+
35+
# We can also use the class name gLFyf to find the google search box.
36+
$SearchBoxElement = Find-SeElement -Driver $Driver -ClassName 'gLFyf'
37+
38+
# This line will get us all elements with the input TagName also known as <input>
39+
$AllInputElements = Find-SeElement -Driver $Driver -TagName input
40+
41+
# The $AllInputElements contains all the input elements on the page if we want to find the specific element for the google search box we will need to loop through each input element in the $AllInputElements array and get the attibute we want in this case we're looking for the title attribute.
42+
# And we only want to return the element that has a title equal to Search we can find this out based on the html code on the page.
43+
$SearchBoxElement = $AllInputElements|%{if($_.GetAttribute('title') -eq 'Search'){return $_}}
44+
45+
# Now for the fun part after finding the element we want to send keyboard input to it. this will allow us to automate the search process
46+
# We can get the list of all special keyboard keys like enter/backspace etc using the Get-SeKeys command
47+
48+
# Now that we can see all the keys we can send send some keys to the SearchBoxElement
49+
Send-SeKeys -Element $SearchBoxElement -Keys 'Powershell-Selenium'
50+
51+
# You can send special key strokes to the SearchBoxElement, you should use the Selenium Keys enum. For example, if we wanted to send an enter key stroke, you could do it like this
52+
Send-SeKeys -Element $SearchBoxElement -Keys ([OpenQA.Selenium.Keys]::Enter)
53+
54+
# When working with dynamic websites, it’s often necessary to wait awhile for elements to appear on the page. By default, Selenium won’t wait and you’ll receive $null from Find-SeElement because the element isn’t there yet. There are a couple ways to work around this.
55+
# The first is to use the Wait-SeElementExists cmdlet to wait for the existence of an element in the document.
56+
# The Wait-SeElementExists will also return the element once it is found so you can use in order to wait and then find elements on the page.
57+
58+
# This command will wait for the img elements for 10 seconds and then return it to you or time out if the element wasn't found on.
59+
$ImageElements = Wait-SeElementExists -Driver $Driver -TagName 'img' -Timeout 10
60+
61+
# Once we have the image element we can simulate a mouse click on it using the Invoke-SeClick command.
62+
Invoke-SeClick -Driver $Driver -Element $ImageElements
63+
64+
# Once we are done with the web driver and we finished with all our testing/automation we can release the driver by running the Stop-SeDriver command.
65+
Stop-SeDriver -Driver $Driver
66+
}
67+
# if the driver is not running we will enter the script block in the else section and display a message
68+
else{
69+
Write-Host "The selenium driver was not running." -ForegroundColor Yellow
70+
}

Selenium.psm1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ function Start-SeChrome {
77
[array]$Arguments,
88
[switch]$HideVersionHint,
99
[System.IO.FileInfo]$DefaultDownloadPath,
10-
[bool]$DisableBuiltInPDFViewer=$true
10+
[bool]$DisableBuiltInPDFViewer=$true,
11+
[bool]$Headless=$false
1112
)
1213

1314
$Chrome_Options = New-Object -TypeName "OpenQA.Selenium.Chrome.ChromeOptions"
@@ -21,6 +22,10 @@ function Start-SeChrome {
2122
$Chrome_Options.AddUserProfilePreference('plugins', @{'always_open_pdf_externally' = $true;})
2223
}
2324

25+
if ($Headless) {
26+
$Chrome_Options.AddArguments('headless')
27+
}
28+
2429
if ($Arguments) {
2530
$Chrome_Options.AddArguments($Arguments)
2631
}

Selenium.tests.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ Describe "Start-SeChrome" {
77
}
88
}
99

10+
Describe "Start-SeChrome headless" {
11+
Context "Should Start Chrome Driver in headless mode" {
12+
$Driver = Start-SeChrome -Headless $true
13+
Stop-SeDriver $Driver
14+
}
15+
}
16+
1017
Describe "Start-SeFirefox" {
1118
Context "Should Start Firefox Driver" {
1219
$Driver = Start-SeFirefox

0 commit comments

Comments
 (0)