Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7009ba4

Browse files
committed
fix examples / remove obsolete
1 parent 43e3c84 commit 7009ba4

File tree

5 files changed

+42
-181
lines changed

5 files changed

+42
-181
lines changed

‎Examples/A-Simple-Google-Search.ps1‎

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,55 @@
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-
101
# 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}
2+
Import-Module -Name Selenium
123

134
# Start the Selenium Chrome Driver
14-
$Driver=Start-SeChrome
5+
Start-SeDriver-Browser Chrome -StartURL 'google.com/ncr'
156

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.
197

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'
238

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.
9+
#Getting the Search box
2710

28-
<# This is the HTML code of the search box
11+
<# This is the HTML code of the search box, found using the browser developer tools
2912
<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-
#>
13+
#>
3114

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
15+
#Here's a few different ways, all valid, to access that searchbox
3416

35-
# We can also use the class name gLFyf to find the google search box.
36-
$SearchBoxElement = Find-SeElement -Driver $Driver -ClassName 'gLFyf'
17+
# 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
18+
#The Single parameter will write an error if there's not 1 element.
19+
$Searchbox = Get-SeElement -By Name -value q -Single
3720

38-
# This line will get us all elements with the input TagName also known as <input>
39-
$AllInputElements = Find-SeElement -Driver $Driver-TagName input
21+
# We can also use the class name gLFyf to find the google search box.
22+
$Searchbox = Get-SeElement -ClassName 'gLFyf'
4023

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|ForEach-Object{if($_.GetAttribute('title') -eq 'Search'){return $_}}
24+
# This line will get us all elements with the input TagName also known as <input>
25+
#The -All parameter (optional) also includes hidden elements.
26+
# The -Attributes parameter will load the specified attribute in the result and make them available through an Attributes property
27+
$AllInputElement = Get-SeElement -By TagName -Value input -All -Attributes title
28+
$AllInputElement | select Attributes
29+
# 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.
30+
# 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.
31+
$Searchbox = $AllInputElement.Where( { $_.Attributes.title -eq 'Search' }, 'first')[0]
4432

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
33+
# 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
34+
# We can get the list of all special keyboard keys like enter/backspace etc using the Get-SeKeys command
4735

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'
36+
# Now that we can see all the keys we can send send some keys to the SearchBoxElement
37+
Invoke-SeKeys -Element $Searchbox -Keys 'Powershell-Selenium'
5038

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)
39+
# 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
40+
# To view special keys, use Get-SeKeys
41+
Invoke-SeKeys -Element $Searchbox -Keys ([OpenQA.Selenium.Keys]::Enter)
5342

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 Find-SeElement cmdlet with the -Wait switch to wait for the existence of an element in the document.
56-
# When using the Find-SeElement with the -Wait please take into account that only 1 element can be returned unlike the without the -Wait switch where multiple elements can be returned.
43+
# 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.
44+
# The first is to use the Find-SeElement cmdlet with the -Wait switch to wait for the existence of an element in the document.
45+
# When using the Find-SeElement with the -Wait please take into account that only 1 element can be returned unlike the without the -Wait switch where multiple elements can be returned.
5746

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-
$LogoImageElement = Find-SeElement -Driver $Driver -Wait -Timeout 10 -Id 'logo'
47+
# 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.
48+
#Note, the value parameter is provided positionally.
49+
$LogoImageElement = Get-SeElement -Timeout 10 -By Id 'logo'
6050

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 $LogoImageElement
51+
# Once we have the image element we can simulate a mouse click on it using the Invoke-SeClick command.
52+
Invoke-SeClick -Element $LogoImageElement
6353

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-
}
54+
# 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.
55+
Stop-SeDriver

‎Examples/DemoSelenium.tests.ps1‎

Lines changed: 0 additions & 54 deletions
This file was deleted.

‎Examples/DemoSelenium2.tests.ps1‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

‎Examples/TestPSGallery.ps1‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎Examples/comparison.ps1‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public void testClass() throws Exception {
88
Assert.assertEquals("Wikipedia. the free encyclopedia", driver.getTitle());
99
}
1010
#>
11-
SeOpen "http://www.wikipedia.org/"
11+
Start-SeDriver-Browser Chrome -StartURL "https://www.wikipedia.org/"
1212
SeShouldHave -Title eq Wikipedia
13-
SeShouldHave 'strong' -By CssSelector -With Text eq 'English' -PassThru | SeClick
13+
SeShouldHave 'strong' -By CssSelector -With Text eq 'English' -PassThru | Invoke-SeClick
1414
SeShouldHave -Title eq 'Wikipedia, the free encyclopedia'
1515

1616
<#
@@ -52,7 +52,9 @@ public static void main(String[] args) {
5252
}
5353
#>
5454

55-
SeOpen "http://demo.guru99.com/test/newtours/" -In FireFox # or #-in Chrome -in MsEdge -in NewEdge or -in IE
55+
Start-SeDriver-Browser Firefox -StartURL "http://demo.guru99.com/test/newtours/"
5656
SeShouldHave -Title eq "Welcome: Mercury Tours"
57-
SeClose
57+
58+
#Stop opened drivers
59+
Get-SeDriver | Stop-SeDriver
5860

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /