標籤:
自動化測試一個SharePoint網站,首先要登陸,我們今天就類比一下使用者登陸SharePoint網站的過程,這一過程可以通過其他方式完成類比,比如通過Coded UI Test錄製指令碼會更方便,但是這裡主要介紹通過API結合Selenium2.0來實現這個過程:
首先我們要建立一個C#的Project,什麼類型的都可以,這裡拿控制台程式舉例,程式如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using OpenQA.Selenium;using OpenQA.Selenium.IE;using OpenQA.Selenium.Support;using OpenQA.Selenium.Support.UI;using Selenium;using mySelenium;using System.Runtime.InteropServices;namespace mySelenium{ class Program { [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetForegroundWindow(); static void Main(string[] args) { IWebDriver driver = new InternetExplorerDriver(); INavigation navigation = driver.Navigate(); navigation.GoToUrl("SharePoint網站url"); driver.FindElement(By.Id("overridelink")).Click(); IntPtr myPtr = GetForegroundWindow(); //IntPtr hWnd = FindWindow(null, "abc"); if (myPtr != IntPtr.Zero) { //Send message to the window. System.Windows.Forms.SendKeys.SendWait("使用者名稱"); System.Windows.Forms.SendKeys.SendWait("{TAB}"); System.Windows.Forms.SendKeys.SendWait("密碼"); System.Windows.Forms.SendKeys.SendWait("{ENTER}"); } } }}
PS:這裡需要引入Selenium 2.0的幾個dll檔案,添加引用:
並引入IEDriverServer.exe,這個過程可以點這裡查看。
然後按F5運行程式即可自動登陸SharePoint網站。
C#中類比使用者登陸SharePoint網站