在 selenium c# 中使用 Sikuli4Net 并禁止 (403)

Using Sikuli4Net in selenium c# and getting a (403) forbidden

在开始之前,我想说,如果有人认为我走错了路,我愿意在我的 selenium 框架内尝试不同的建议。很长一段时间以来,我一直很高兴地开发我的 selenium 测试套件(c#、Nunit),不幸的是在尝试实施 Sikuli4Net 时遇到了一些非常令人沮丧的事情。

我的项目需要引入一个 canvas 来以图形方式绘制数据流图并与之交互,但这增加了复杂性,因为没有任何常用的 selenium 用于识别屏幕上的对象.我去过几个论坛,他们似乎都建议使用 Sikuli4Net 以图形方式与之交互。

我开始关注一些 YouTube 视频,但我似乎什么都做不了。每次我 运行 我的测试都会得到以下堆栈跟踪:

   at System.Net.HttpWebRequest.GetResponse()
      at Sikuli4Net.sikuli_REST.Screen.MakeRequest(String requestURLExtension, String jsonObject)
      at Sikuli4Net.sikuli_REST.Screen.Click(Pattern pattern, KeyModifier kmod, Boolean highlight)
      at Builder2018.SmokeTests.canvas.canvasBob(String BrowserName, String Environment, String System) in C:\Users\Administrator\Documents\Builder2018\Builder2018\Builder2018\SmokeTests\canvas.cs:line 38
   Result Message:  System.Net.WebException : The remote server returned an error: (403) Forbidden.

我暂时放弃了尝试在我的实际项目中实现它,我只是想使用 Google 获得一个 hello world 类型的示例,但仍然出现上述错误。有没有什么简单的东西我错过了,因为它不应该这么难...

据我所知,我已经完成了所有必要的配置(JDK、环境变量等),这是我目前的尝试:

 namespace AutomationStuffs.SmokeTests
 {
     [TestFixture]
     public class canvas : TestBase
     {
         APILauncher launcher = new APILauncher();
         [Test]
         [TestCaseSource(typeof(TestBase), "TestData")]
         public void canvasBob(String BrowserName, String Environment, String System)
         {
             //Start API Launcher
            launcher.Start();
            //Configure test setup - within my environment i would usually perform a repeated login step here within setup
             Setup(BrowserName, Environment, System, new string[] { });
            //GoTo Google
             KnowledgeBasePage.GoToGoogle();
            //Test steps i have issue with
                Screen screenGoogle = new Screen();
                Pattern element1 = new Pattern(@"C:\Users\Administrator\Documents\AutomationStuffs\AutomationStuffs\PageImages\feelinglucky.PNG");
                screenGoogle.Click(element1);
             //Start API Launcher
            launcher.Stop();
         }
     }
 }

为了完整起见 - 我已经包括了其他适当的被调用的位:

  public void Setup(String BrowserName, String Environment, String System, String[] Additional)
  {
      //launcher.Start();
      Driver.Intialize(BrowserName);
    //perform SQL inserts and set appropriate data based on BrowserName, Environment, System, Additional..
      //LoginSteps(); //not used for this example test
  }

 public static void Intialize(String BrowserName)
 {
     String DRIVER_PATH = chromeDriver;
     String DRIVER_PATH_IEFF = ieDriver;
     String DRIVER_PATH_EDGE = edgeDriver;
     var optionsChrome = new ChromeOptions();
     optionsChrome.AddArgument("start-maximized");
     optionsChrome.AddArgument("no-sandbox");
     var opt = new FirefoxOptions
     {
         BrowserExecutableLocation = @"c:\program files\mozilla firefox\firefox.exe"
     };
     //var driver = new FirefoxDriver(opt);
     var optionsIe = new InternetExplorerOptions();

     if (BrowserName.Equals("ie"))
     {
         Instance = new InternetExplorerDriver(DRIVER_PATH_IEFF);
         Instance.Manage().Window.Maximize();
     }
     else if (BrowserName.Equals("firefox"))
     {
         Instance = new FirefoxDriver(opt);
         Instance.Manage().Window.Maximize();
         Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
     }
     else if (BrowserName.Equals("edge"))
     {
         Instance = new EdgeDriver(DRIVER_PATH_EDGE);
         Instance.Manage().Window.Maximize();
     }
     else if (BrowserName.Equals("chrome"))
     {
         Instance = new ChromeDriver(DRIVER_PATH, optionsChrome);
         Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
     }
     else
     {
         //Your test has failed because an unsupported browser-name was selected!
         Assert.Fail("Your test has failed because an unsupported browser-name was selected! Currently supported browser names in the BrowsersList.resx file are: ie,firefox,edge,chrome - these are Case sensitive");
     }
 }

只是想我会留下一些关于我在哪里处理的信息。基本上,我转而使用 SikuliIntegrator。

代码更简单,没有 API 启动器。我能够更轻松地重构我的代码,并将执行操作的实际代码与我的测试分开,使它们易于阅读。唯一的缺点是启动时的等待时间,它会增加大约 2 到 7 秒的测试时间。我使用的测试和代码如下所示:

        namespace AutomationStuffs.SmokeTests
        {
            [TestFixture]
            public class canvas : TestBase
            {
                [Test]
                [TestCaseSource(typeof(TestBase), "TestData")]
                public void canvasBob(String BrowserName, String Environment, String System)
                {
                    //Configure test setup - within my environment i would usually perform a repeated login step here within setup
                    Setup(BrowserName, Environment, System, new string[] { });
                    //GoTo Google
                    KnowledgeBasePage.GoToGoogle();
                    GooglePage.ClickImFeelingLucky();
                    Assert.IsTrue(googlePage.OnTheImFeelingLuckyPage());
                }
            }
        }

    public static void ClickImFeelingLucky()
    {
        Wait.WaitForPageElementToBeVisible(By.Name("q"), 10);
        string MyPicture = @"C:\..\intractionImages\Capture.PNG";
        Console.WriteLine(MyPicture);
        SikuliAction.Click(MyPicture);
        Wait.WaitForPageElementToBeVisible(By.Id("lang-chooser"), 10);
    }

在这里找到了所有有用的信息https://qaagent.wordpress.com/2013/07/17/how-to-use-sikuli-in-my-c-coded-tests/

这是文章的主要内容:

确保 Java 配置正确: 要求

  • java 1.6 – 使用 jre-6u35-windows-i586.exe

  • 测试
  • 微软.NET 4.5

  • NuGet

Add JAVA_HOME environment variable with value = path to installed JRE bin folder.

安装 您可以获得一个 StartUp 项目,或者可以按照以下步骤创建一个新项目:

  1. 在 Visual Studio 2012 年创建新项目

  2. 在“解决方案资源管理器”中——右击解决方案

  3. 选择“管理解决方案的 NuGet 包...”

  4. 搜索“SikuliIntegrator”并安装

  5. 安装后“JSikuliModule.jar”应该是您解决方案的一部分,还有一些附加文件

  6. 右击“JSikuliModule.jar”并选择“属性”

  7. 将“复制到输出目录”属性设置为“始终复制”

我发现第 6 步和第 7 步是整个过程中最重要的部分

这是我发现有用的额外阅读清单: