如何在Powershell模块文件中使用类型加速器?

时间:2018-12-19 07:08:05

标签: powershell

我正在使用Powershell ISE。我有一个要导入的硒类。为了正确运行此脚本,我需要在哪里放置类型加速器?编写此程序的正确方法是什么?我是Powershell的新手,请帮助。 我尝试将类型加速器放在 TestByClassKeyWord.ps1 文件中,但是没有用。

主文件

PoC.Test.ps1

try
{
    Add-Type -Path (Join-Path C:\WORK\PoC 'WebDriver.dll')
    Add-Type -Path (Join-Path C:\WORK\PoC 'WebDriver.Support.dll')

    C:\WORK\PoC\TestByClassKeyWord.ps1
}
catch [System.Exception]
{
    Write-Output "exception is: $($PSItem.Exception.Message)"
}
finally
{

}

下面是我正在调用的脚本

TestByClassKeyWord.ps1

using module 'C:\WORK\PoC\PgUserLogin.psm1'
using module 'C:\WORK\PoC\PgMain.psm1'

#Selenium trigger from Powershell
$driver = [ChrmDriver]::new()
try
{
    $pgUserLogin = [PgUserLogin]::new($driver)
    $pgUserLogin.SignIn('admin', 'admin')
}
catch [System.Exception]
{
    throw [System.Exception] "$PSItem.Exception.Message."
}
finally
{
    $driver.Quit()
}

一个模块文件,

PgUserLogin.psm1

# type accelerators
$accelerators = [PowerShell].Assembly.GetType('System.Management.Automation.TypeAccelerators')
$accelerators::Add('SelWait','OpenQA.Selenium.Support.UI.ExpectedConditions')
$accelerators::Add('SelBy','OpenQA.Selenium.By')
$accelerators::Add('SelKeys','OpenQA.Selenium.Keys')
$accelerators::Add('WbDrvWait','OpenQA.Selenium.Support.UI.WebDriverWait')
$accelerators::Add('RmtWbElement','OpenQA.Selenium.Remote.RemoteWebElement')
$accelerators::Add('ChrmDriver','OpenQA.Selenium.Chrome.ChromeDriver')

class PgUserLogin
{
    #region Fields


    [ChrmDriver] $driver
    [WbDrvWait] $wait
    #endregion

    #region Constructors
    PgUserLogin($driver)
    {
        $this.driver = $driver
        # Chrome driver
        $this.driver.Navigate().GoToUrl('https://10.88.12.77/userlogin.html')
        $this.driver.Manage().Window.Maximize()
        $this.wait = [WbDrvWait]::new($this.driver, 5000)
    }
    #endregion

    #region UIElements

    [RmtWbElement] tbUserName()
    {
        $this.wait.Until([SelWait]::ElementIsVisible([SelBy]::Id('cred_userid_inputtext')))
        return $this.driver.FindElementById('cred_userid_inputtext')
    }

    [RmtWbElement] tbPassword()
    {
        $this.wait.Until([SelWait]::ElementIsVisible([SelBy]::Id('cred_password_inputtext')))
        return $this.driver.FindElementById('cred_password_inputtext')
    }

    [RmtWbElement] btnSignIn()
    {
        $this.wait.Until([SelWait]::ElementIsVisible([SelBy]::XPath("//button[@label='Sign In']")))
        return $this.driver.FindElementByXPath("//button[@label='Sign In']")
    }
    #endregion

    #region UIActions

    [void] SignIn($userName, $password)
    {
        $this.tbUserName().SendKeys($userName)
        $this.tbPassword().SendKeys($password)
        $this.btnSignIn().Click()
    }
    #endregion
}

我遇到错误-

DBG]: PS C:\WINDOWS\system32>> 
exception is: At C:\WORK\PoC\PgUserLogin.psm1:15 char:6
+     [ChrmDriver] $driver
+      ~~~~~~~~~~
Unable to find type [ChrmDriver].

At C:\WORK\PoC\PgUserLogin.psm1:16 char:6
+     [WbDrvWait] $wait
+      ~~~~~~~~~
Unable to find type [WbDrvWait].

At C:\WORK\PoC\PgUserLogin.psm1:26 char:23
+         $this.wait = [WbDrvWait]::new($this.driver, 5000)
+                       ~~~~~~~~~
Unable to find type [WbDrvWait].

1 个答案:

答案 0 :(得分:0)

类型加速器应该放在using语句之前。