• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

自动化测试一个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网站。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap