【问题标题】:Calling data from one class to another class - framework for JUnit从一个类调用数据到另​​一个类 - JUnit 框架
【发布时间】:2014-04-28 13:33:09
【问题描述】:

请原谅我,因为我对测试自动化的世界还很陌生。我开始使用 Selenium WebDriver 和 JUnit4,主要是在 Windows 操作系统上,尽管我修改了我的脚本并在 Mac 上运行它们。

我希望能够创建一组包含设置数据的类,例如用户名、密码、默认 url。甚至可能从 excel 文件中调用它们,但现在我很乐意将数据存储在类中,然后将这些数据传递给其他测试类。我猜这将是某种框架。

目前我正在编写的类都以这样的开头:

public class ExampleSQATest{

    public static Chromedriver chrome;

    @BeforeClass
    public static void launchBrowser(){
    System.setProperty("webdriver.chrome.driver", "chromedriver/chromedriver.exe");

    chrome = new ChromeDrievr();
    }
    @Test
    public void aLogin(){

    chrome.manage().window().maximize();

    chrome.navigate().to("http://mydummywebsite.com");

    new WebDriverWait(chrome, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector
                    ("input#UserName")));

    WebElement username = chrome.findElementByCssSelector("input#UserName");

    username.sendKeys("username");

    WebElement password = chrome.findElementByCssSelector("input#Password");
    password.sendKeys("password");
    WebElement submit = chrome.findElementByCssSelector("input[type='submit']");
    submit.click();
    }
}

然后我将继续编写需要输入数据的进一步测试方法,但我希望能够从已经预定义的其他地方调用这些数据。

任何人都可以提供任何合适的建议进行调查,以便我学习。作为指南或教程的东西。没有什么太高级的东西,只是通过建议我如何设置一个由其他类调用的方法类以及如何将它们作为一个框架链接在一起来帮助我入门。

非常感谢。

【问题讨论】:

    标签: selenium-webdriver junit4


    【解决方案1】:

    一种方法

    public abstract class TestBase
    {
    
        private readonly INavigationManager navMgr;
        private readonly IWindowNavigator windowNav;
        private readonly ILoginManager loginMgr;
        // All your stuff that is common for all the tests
        protected TestBase()
        {
            this.navMgr = WebDriverManager.Get<INavigationManager>();
            this.windowNav = WebDriverManager.Get<IWindowNavigator>();
            this.loginMgr = WebDriverManager.Get<ILoginManager>();
        }}
    
     [TestFixture]
    internal class QueriesTest : TestBase
    {
        private QueryTests queryTests;
    
        [SetUp]
        public void Setup()
        {
            this.queryTests = WebDriverManager.Get<QueryTests>();
          // all the stuff you run specific before tests in this test class.
        }
    
    }
    

    【讨论】:

    • 代码是否适用于 C# 而不是 Java?
    • @Django_Tester 将其视为伪代码,只是为了让您了解如何解决此类问题。
    【解决方案2】:

    假设你已经在 webdriver-junit4 中创建了测试类,使用以下两个类来调用你的测试类(Note-Import junit annotations)

    1) 将测试套件类创建为 -

    @RunWith(Suite.class)
    @Suite.SuiteClasses({
    YourTestClass1.class,
    YourTestClass2.class,[you can add more tests you have created...]
    })
    public class TestSuiteJU {   
    } 
    

    2)创建类以调用上面创建的套件-

    public class TestExecution {
    public static void main(String[] args) {
    Result result = JUnitCore.runClasses(TestSuiteJU .class);
    }
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      相关资源
      最近更新 更多