zoukankan      html  css  js  c++  java
  • 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网站。

  • 相关阅读:
    #与javascript:void(0)的区别
    单选框、复选框、下拉列表
    数据类型后的“?”
    c#中日期的处理
    日期控件html
    javascript获取后台传来的json
    Hashtable语法简介
    Hashtable(哈希表)
    Dictionary 字典
    远程SQL Server连接不上
  • 原文地址:https://www.cnblogs.com/LanTianYou/p/4522099.html
Copyright © 2011-2022 走看看