C#中模拟用户登陆SharePoint网站

自动化测试一个SharePoint网站,首先要登陆,我们今天就模拟一下用户登陆SharePoint网站的过程,这一过程可以通过其他方式完成模拟,比如通过Coded UI Test录制脚本会更方便,但是这里主要介绍通过API结合Selenium2.0来实现这个过程:

首先我们要创建一个C#的Project,什么类型的都可以,这里拿控制台程序举例(需要引入System.Windows.Forms包),程序如下:

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网站。

posted @ 2015-05-22 13:27  天外归云  阅读(888)  评论(0编辑  收藏  举报