• 自动化测试:Selenium webdriver 学习笔记-C#版(二)


      前面环境搭建好了,那么下面我们来了解一些简单的应用

      1>进入指定的网页

     driver.Navigate().GoToUrl("url")

      

      2>设置页面大小

    driver.Manage().Window.Size = new System.Drawing.Size(w h);

      3>页面前进,后退

    driver.Navigate().Forward();
    driver.Navigate().Back();

      4>截图

    driver.GetScreenshot().SaveAsFile()

      

      5>退出

    driver.Quit()

       

    下面看具体的实例 

    进入百度首页,将浏览器设置固定大小,然后将其设为最大,输入搜索内容,点击搜索按钮,对搜索结果进行截图。 

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading;
    //添加selenium的引用
     using OpenQA.Selenium;
     using OpenQA.Selenium.IE;
     using OpenQA.Selenium.Support.UI; 
     //添加引用-在程序集中添加System.Drawing
     using System.Drawing.Imaging;
     
     
     namespace Selenium
     {
        class Program
        {
            static void Main(string[] args)
            {
                 //此时记得添加路径    
                using (var driver = new InternetExplorerDriver(@"D:SeleniumIEDriverServer_x64_2.34.0")) 
                 {
                     //进入百度首页
                     driver.Navigate().GoToUrl(@"http://www.baidu.com");
                     Thread.Sleep(1000);
     
                     //设置固定宽,高
                     driver.Manage().Window.Size = new System.Drawing.Size(100, 200);
                     Thread.Sleep(1000);
     
                     //设置窗体最大化
                     driver.Manage().Window.Maximize();
                     Thread.Sleep(1000);
     
                     //找到对象
                     var searchBox = driver.FindElementById("kw1");
                     var btnClick = driver.FindElementById("su1");
     
                     //发送搜索内容
                     searchBox.SendKeys("selenium");
                     Thread.Sleep(1000);
     
                     //点击按钮
                     btnClick.Click();
                     Thread.Sleep(1000);
     
                     //后退到百度首页
                     driver.Navigate().Back();
                     Thread.Sleep(1000);
     
                     //回到新闻页
                     driver.Navigate().Forward();
                     Thread.Sleep(1000);
     
                     //截图
                     //自动化测试中截图的图片用当前时间来命名,会起到非常不错的效果
                     string pictrueName = DateTime.Now.ToString();
                     if (pictrueName.Contains(':'))
                     {
                         pictrueName = pictrueName.Replace(':', '_');
                     }
                     if (pictrueName.Contains('/'))
                     {
                         pictrueName = pictrueName.Replace('/', '_');
                     }
     
                     driver.GetScreenshot().SaveAsFile(@"D:" + pictrueName + ".Jpeg", ImageFormat.Jpeg);
                     Thread.Sleep(1000);
     
                     //退出
                     driver.Quit();
                 }
             }
         }
     }
  • 相关阅读:
    获取AppSettings配置,获取连接字符串
    类在初始化的时候做了什么事
    Easyui Tabs 添加怎么添加。
    Tree数据格式 Easyui
    使用CodeFirst建表的时候要知道的特性
    从数据导出模型到pd设计器
    orm的几种排序写法
    Parallel.ForEach 并行循环的使用
    kendo gird 刷新数据源的几种方式
    表格设置宽度在ie9上无效
  • 原文地址:https://www.cnblogs.com/Alvin-x/p/3557999.html
Copyright © 2020-2023  润新知