精選文章

bcp大量匯出與匯入資料庫資料方法

--匯出-- bcp "select * from [資料庫名稱].[dbo].[資料表名稱]" queryout 匯出檔案名稱.txt -w -U "使用者帳號" -P "使用者密碼" " ...

顯示具有 自動測試-Selenium 標籤的文章。 顯示所有文章
顯示具有 自動測試-Selenium 標籤的文章。 顯示所有文章

2019年10月1日 星期二

Selenium-瀏覽器的操作

說明:
以下是一些關於瀏覽器操作的語法與案例

1.訪問指定網址
以下二種方法都可以訪問指定的網址
driver.Navigate().GoToUrl("https://www.google.com/");

driver.Url ="https://www.google.com/"

2.回上一頁
driver.Navigate().Back();

3.到下一頁
driver.Navigate().Forward();

4.重新整理
driver.Navigate().Refresh();

5.瀏覽器最大化
driver.Manage().Window.Maximize();

6.瀏覽器最小化
driver.Manage().Window.Minimize();

7.瀏覽器自訂大小
driver.Manage().Window.Size = new System.Drawing.Size(800, 600);

8.關閉瀏覽器
driver.close(); //關閉當前瀏覽器視窗
driver.quit(); //退出驅動並關閉所有關聯的視窗

2019年9月26日 星期四

Selenium-使用不同的瀏覽器

說明:
在前篇初始語法裡,使用瀏覽器是Chrome,但Selenium支援很多不同的瀏覽器, 這裡要教如何改更瀏覽器,可以實現同個案例,可以使用不同的瀏覽器進行驗證。

驅動瀏覽器
在初始語法裡,是寫在 [ClassInitialize],
就是每次執行此測試類別時,都會去執行,驅動Chrome如下
driver = new ChromeDriver();
想要使用其他瀏覽器,只要更改此段語法即可,在更改其他瀏覽器時,在using也要新增該瀏覽器的提示詞。

以第1個範例的程式碼進行修改-使用Firefox
將語法修改如下:
using OpenQA.Selenium.Chrome; => using OpenQA.Selenium.Firefox;
driver = new ChromeDriver(); => driver = new FirefoxDriver();
並執行測試,結果發生錯誤訊息,原因是,沒有安裝Firefox驅動,可以到NuGet安裝「Selenium.Firefox.WebDriver」
相關的瀏覽器驅動可以到https://www.seleniumhq.org/download/,有下載網址。
錯誤訊息:
類別初始設定方法 UnitTestProject1.UnitTest1.InitializeClass 擲回例外狀況。OpenQA.Selenium.DriverServiceNotFoundException: OpenQA.Selenium.DriverServiceNotFoundException: The geckodriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases.。
使用IE
操作與Firefox相同,也要去下載驅動,都完成後執行測試,還是發生錯誤訊息,
需要去調整IE的安全性,操作如下:
工具->網際網路選項->安全性,
將「網際網路」、「近端內部網路」、「信任的網站」、「限制的網站」
以上四個區域的「啟用受保護模式(需要重新啟動Internet Explorer)」,
都打勾或都不勾

錯誤訊息:
System.InvalidOperationException: System.InvalidOperationException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (SessionNotCreated)。

2019年9月20日 星期五

Selenium-開始第1個範例 (for C# MSTest)

說明:
使用初始語法去修改,在T1內容裡輸入以下內容後執行測試, 執行成功的話,會直接關掉瀏覽器,建議可以把
CleanupClass()中的
driver.Close()
driver.Dispose()
註解掉

範例
會導向google首頁網址
    
 public void T1名稱()
        {
            try
            {
                driver.Navigate().GoToUrl("https://www.google.com");
            }
            catch (Exception ex)
            {
                verificationErrors.Append(ex);
            }
               
        }

結果影片

2019年9月19日 星期四

Selenium-初始語法 (for C# MSTest)

說明:
使用C# MSTest 進行Selenium時,會這樣設計初始語法再去新增或修改

 
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.Diagnostics;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        //初始設定
        private static IWebDriver driver;
        private StringBuilder verificationErrors;

        // ClassInitialize運行類別的第一個測試前運行代碼(在這個測試類別都會去執行)
        [ClassInitialize]
        public static void InitializeClass(TestContext testContext)
        {
            //自行設定路徑或直接從Nuget安裝
            driver = new ChromeDriver();
            //瀏覽器最大化
            driver.Manage().Window.Maximize();
        }
        // ClassCleanup運行完類別中的所有測試后再運行代碼
        // (就是這個測試類別裡的測試代碼都執行完畢後才執行)
        [ClassCleanup]
        public static void CleanupClass()
        {
            try
            {
                //driver.Quit();// quit does not close the window
                driver.Close();
                driver.Dispose();
            }
            catch (Exception)
            {
                // Ignore errors if unable to close the browser
            }
        }
        // TestInitialize在運行每個測試前先運行代碼(就是在TestMethod()前會執行的程式碼)
        [TestInitialize]
        public void InitializeTest()
        {
            verificationErrors = new StringBuilder();
        }
        // TestCleanup在運行完每個測試後運行代碼(就是在TestMethod()後會執行的程式碼)    
        [TestCleanup]
        public void CleanupTest()
        {
            //Trace.WriteLine(verificationErrors.ToString());
            Assert.AreEqual("", verificationErrors.ToString());
        }
        // TestMethod撰寫測試案例主要內容,[T1]是為了執行順序方便排序使用,[名稱]可以編輯
        [TestMethod]
        public void T1名稱()
        {

        }
        [TestMethod]
        public void T2名稱()
        {
            
        }

    }
}



2018年10月23日 星期二

Selenium-安裝方式 (for C# MSTest)

l   相關套件下載:
n   Selenium.WebDriver
n   Selenium.Support
n   Selenium.WebDriver.ChromeDriver(Chrome瀏覽器使用)
l   安裝方式:使用C#,於NuGet進行下載安裝
n   步驟:ToolsàNuGet Package ManageràManage NuGet Packages for Solution

n   步驟:點選Browse,於search輸入Selenium

n   步驟:選擇Selenium.WebDriver安裝(版本:3.14.0 安裝時間:2018/10/08)
     
安裝完畢後,再進行後續安裝。
n   步驟:選擇Selenium.Support安裝(版本:3.14.0 安裝時間:2018/10/08)
     
安裝完畢後,再進行後續安裝。
n   步驟:選擇Selenium.Chrome.WebDriver安裝(使用Chrome 瀏覽器)
(
版本:2.42.0 安裝時間:2018/10/08)安裝完畢後,再進行後續安裝。
(其他瀏覽器可安裝清單如下:
Selenium.Firefox.WebDriver
Firefox
Selenium.WebDriver.IEDriverIE
l   安裝方式:使用C#,另外下載driver
n   步驟:下載的driver可存放於一個資料夾內
n   步驟:要使用時只要在宣告時在dirver後面填入路徑即可,參考如下:
driver = new InternetExplorerDriver(@"C:\temp\driver")
l   安裝方式:使用C#,開啟IE瀏覽器的方法
說明:要使用IE瀏覽器進行自動測試需完成以下步驟,不然會無法使用IE
n   步驟:可以使用NuGet或自行到相關網站下載,如上面說明方式
n   步驟:開啟IEà網際網路選項à安全性
將「網際網路」、「近端內部網路」、「信任的網站」、「限制的網站」
以上四個區域的「啟用受保護模式(需要重新啟動Internet Explorer)」,
都打勾或都不勾。
n   步驟:此處是使用Win10 X64環境下IE11版本,有此選項,如未有此項目請自行參考Google有其他文章教學。
而「啟用加強的受保護模式」不要勾選,
當勾選時,一樣可以執行,但測試案例會一直失敗有時會成功。
不勾選時,畫面會出「This is the initial start page for the WebDriver server.
但不是錯誤而是正在執行。

n   步驟:瀏覽器的縮放比例必須為100%,不然執行測試案例時會一直出現錯誤訊息!

n   步驟:網路上還教學修改登錄檔的部份,但實際測試不加也可以使用,所以此為補充內容:
32Windowskey值為
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InternetExplorer\Main\FeatureControl\FEATURE_BFCACHE
64Windowskey值為
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\InternetExplorer\Main\FeatureControl\FEATURE_BFCACHE
不存在請新增。之後在key建立一個iexplore.exeDWORD類型,值為0