简体   繁体   English

相当于Java类级别字段的C#

[英]C# equivalent of Java class level fields

I'm new(er) to C# than I am to Java and I'm attempting to convert some of my java automation tests into C#. 我是C#的新手,而不是Java的新手,并且我正尝试将我的某些Java自动化测试转换为C#。
My question is about following code: 我的问题是关于以下代码:

public class UnitTest1
{ 
    [Test]
    public void loginTest()
    {
        //path variable points to the chrome driver exe bin location
        var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        //pass the path to our new driver
        var driver = new ChromeDriver(path);
        //then pass the driver to our page object + add IWebdriver as a 
        //param to HomepageClass obj
        var homePage = new HomePage(driver);

        homePage.goTo();
    }
}

This code instantiates a new chrome driver and calls a simple goTo method on a homepage object. 这段代码实例化了一个新的chrome驱动程序,并在首页对象上调用了一个简单的goTo方法。 It also passes the driver to the homepage Object allowing the driver to interact with the page object in other ways. 它还将驱动程序传递给主页对象,从而允许驱动程序以其他方式与页面对象进行交互。

The way that the code is currently written means that I would have to recreate the local variables for every future Test that I plan to script. 当前编写代码的方式意味着我将不得不为计划编写的每个将来的Test重新创建局部变量。 In a similar fashion to Java, I would like to use something equivalent to fields at class level in order for them to be reused across multiple tests in the class. 以与Java类似的方式,我想在类级别使用与字段等效的东西,以便在类中的多个测试之间重用它们。 Can anyone point me in the right direction on how to go about this? 谁能为我指出正确的方向?

What I've tried so far: 到目前为止,我已经尝试过:

I tried changing the variables to objects at class level, and initializing them in a setup method like so: 我尝试将变量更改为类级别的对象,并使用如下设置方法对其进行初始化:

public class UnitTest1
{
    object homePage;
    object path;
    object driver;

    [SetUp]
    public void startDriver()
    {
        //path variable points to the chrome driver bin location
        path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        //pass the path to our new driver
        driver = new ChromeDriver(path);
        //then pass the driver to our page object + add 
        //IWebdriver as a param to HomepageClass obj
        homePage = new HomePage(driver);
    }

    [Test]
    public void loginTest()
    {
        homePage.goTo();
    }
}

This generated the following errors, some of which I managed to fix as follows: 这产生了以下错误,我设法将其中一些错误修复如下:

Argument 1: cannot convert from 'object' to 'OpenQA.Selenium.Chrome.ChromeOptions' 参数1:无法从“对象”转换为“ OpenQA.Selenium.Chrome.ChromeOptions”

State: fixed by: Changing: object path; 状态:固定方式:更改: object path; to ChromeOptions path; ChromeOptions path;

Cannot implicitly convert type 'string' to 'OpenQA.Selenium.Chrome.ChromeOptions' 无法将类型“字符串”隐式转换为“ OpenQA.Selenium.Chrome.ChromeOptions”

State: Not yet fixed. 州:尚未确定。 This was generated as a result of fixing the first error 这是修复第一个错误的结果

Argument 1: cannot convert from 'object' to 'OpenQA.Selenium.IWebDriver' 参数1:无法从“对象”转换为“ OpenQA.Selenium.IWebDriver”

State: fixed by : Changing object driver; 状态:固定于:更改object driver; to IWebdriver driver; IWebdriver driver;

'object' does not contain a definition for 'goTo' and no extension method 'goTo' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) “对象”不包含“ goTo”的定义,找不到找不到接受类型为“ object”的第一个参数的扩展方法“ goTo”(是否缺少using指令或程序集引用?)

State: not yet fixed 状态:尚未确定

Any advice appreciated 任何建议表示赞赏

Note that this is untested, and only based on your errors: 请注意,这未经测试,仅基于您的错误:

Your Path.GetDirectoryName(...) returns a string , so instead of object path; 您的Path.GetDirectoryName(...)返回一个string ,所以不是object path; or ChromeOptions path; ChromeOptions path; it should be string path; 它应该是string path; instead. 代替。

As for your last quoted error: 至于您最后引用的错误:

'object' does not contain a definition for 'goTo' and no extension method 'goTo' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) “对象”不包含“ goTo”的定义,找不到找不到接受类型为“ object”的第一个参数的扩展方法“ goTo”(是否缺少using指令或程序集引用?)

This means you are currently trying to use the .goTo on the object homePage , and the object -class obviously doesn't has any goTo method. 这意味着您当前正在尝试在object homePage上使用.goTo ,而object -class显然没有任何goTo方法。
This can be fixed by either changing object homePage; 可以通过更改object homePage; to HomePage homePage , or alternatively change homePage.goTo(); HomePage homePage ,或更改homePage.goTo(); to ((HomePage)homePage).goTo(); ((HomePage)homePage).goTo(); (I would suggest the first). (我建议第一个)。

Not sure where the first quoted error is coming from, since I don't see any ChromeOptions in your startDriver() method, but I would also change object driver; 由于我在startDriver()方法startDriver()不到任何ChromeOptions ,因此不确定第一个引用的错误来自startDriver() ,但是我还将更改object driver; to ChromeDriver driver; ChromeDriver driver; .


TL;DR: TL; DR:

Change: 更改:

object homePage;
object path;
object driver;

to: 至:

HomePage homePage;
string path;
ChromeDriver driver;

PS: Similar errors would occur in Java when you would have Object variables on class-level without using casts in the code. PS:如果在类级别上具有Object变量而不在代码中使用强制转换,则在Java中会发生类似的错误。

The first error you received: 您收到的第一个错误:

Argument 1: cannot convert from 'object' to 'OpenQA.Selenium.Chrome.ChromeOptions' 参数1:无法从“对象”转换为“ OpenQA.Selenium.Chrome.ChromeOptions”

wasn't so much telling you to change the type of your path variable to ChromeOptions , but was telling you that the ChromeDriver constructor takes a type of ChromeOptions rather than a generic object . 并不是告诉您将路径变量的类型更改为ChromeOptions ,而是告诉您ChromeDriver构造函数采用的是ChromeOptions类型,而不是通用object

The documentation is decent for this and should always be the first place you look when working with unfamiliar types ( https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/chrome/ChromeDriver.html ) 该文档对此很不错,并且在使用不熟悉的类型时,它应该始终是您看的第一手( https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/chrome/ChromeDriver.html

Based on the documentation for ChromeOptions , you can specify the path to the Chrome binary by doing the following: 根据ChromeOptions的文档,您可以通过执行以下操作来指定Chrome二进制文件的路径:

ChromeOptions options = new ChromeOptions()
options.setBinary(new File(path));

You can always move the options variable to the class level and initialise once if that's what you want to do. 您始终可以将options变量移到类级别,如果要这样做,则可以初始化一次。

To expand on Kevin's answer - it's generally best not to simply declare something as an object unless absolutely necessary as you won't be able to call any type specific methods on the object without casting it first 为了扩展Kevin的答案-除非绝对必要,否则通常最好不要简单地将某些东西声明为对象,因为如果不先进行转换就无法在该object上调用任何特定于类型的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM