How do I make my variables available to all steps of my tests



  • I'm using data faker and I would like to know how to make my variables available in all steps of my test.

    I wanted to leave all elements mapped in the initial step, or be given

    WebDriver driver = new ChromeDriver();  
    
     //Mapping the elements
     WebElement nomeusuario = driver.findElement(By.className("whsOnd zHQkBf"));
    

    My main code is:

    public class Criar_Conta_Gmail {
    
    @Given("^que eu esteja na pagina principal do gmail\\.$")
    public void que_eu_esteja_na_pagina_principal_do_gmail() throws Throwable {
        System.setProperty("webdriver.chrome.driver","C:\\Browsers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.gmail.com");
        driver.findElement(By.xpath("//span[contains(text(),'Criar conta')]")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//div[contains(text(),'Para mim')]")).click();
        throw new PendingException();
      }
    
    @When("^criar uma nova conta com telefone invalido\\.$")
    public void criar_uma_nova_conta_com_telefone_invalido() throws Throwable {
            WebDriver driver = new ChromeDriver();  
    
         //Mapping the elements
         WebElement nomeusuario = driver.findElement(By.className("whsOnd zHQkBf"));
    
        // sending data to the application
        Faker faker = new Faker();
        String Nome = faker.name().firstName();
        nomeusuario.sendKeys(Nome);
    
        throw new PendingException();
    }
    
    @Then("^o sistema não permite a finalização do cadastro\\.$")
    public void o_sistema_não_permite_a_finalização_do_cadastro() throws Throwable  {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
    


  • Is it unique to this class, or will each class need their own? If it's just in this class, I would make properties for each piece of data that I'm going to need multiple times.

    Something like

            private string _fakeName;
            private string FakeName
            {
                get
                {
                    if (string.IsNullOrEmpty(_fakeName))
                    {
                        Faker faker = new Faker();
                        _fakeName = faker.name().firstName();
                        return _fakeName;
                    }
                    return _fakeName;
    
                }
            }
    
    
            private IWebDriver _driver;
            public IWebDriver driver
            {
                get
                {
                    if (_driver == null)
                        _driver = new ChromeDriver();
                    return _driver;
    
                }
            }
    
            public IWebElement nomeusuario
            {
                get
                {
                    return driver.FindElement(By.ClassName("whsOnd zHQkBf"));
                }
            }
    
    ```
    
    
    

    If you want the name to change with each test, make the setup change _fakename to null. If you want all of your tests to use it, I would recommend creating a fake data class, which is what I would normally do if the tests will be run synchronously. That way, I can create and recreate the data whenever I want. Additionally, using the above, when you attempt to use driver, it will always use the same one, and when you call nomeusuario. With this, it will always do the lookup each time you call the element, and you do run the risk of it becoming stale. If you really want to use the one lookup, and never again, use the below

            private IWebElement _nomeusuario;
            public IWebElement nomeusuario
            {
                get
                {
                    if(_nomeusuario==null)
                        _nomeusuario = driver.FindElement(By.ClassName("whsOnd zHQkBf"));
                    return _nomeusuario;
                }
            }
    
    ```
    
    
    

    Now, as long as you are calling from within the same class, you can do stuff like the below, just modifying your original code.

    
    
    public class Criar_Conta_Gmail {
            private string _fakeName;
            private string FakeName
            {
                get
                {
                    if (string.IsNullOrEmpty(_fakeName))
                    {
                        Faker faker = new Faker();
                        _fakeName = faker.name().firstName();
                        return _fakeName;
                    }
                    return _fakeName;
    
                }
            }
    
    
            private IWebDriver _driver;
            public IWebDriver driver
            {
                get
                {
                    if (_driver == null)
                        _driver = new ChromeDriver();
                    return _driver;
    
                }
            }
    
    
    
            private IWebElement _nomeusuario;
            public IWebElement nomeusuario
            {
                get
                {
                    if(_nomeusuario==null)
                        _nomeusuario = driver.FindElement(By.ClassName("whsOnd zHQkBf"));
                    return _nomeusuario;
                }
            }
    
    
    @Given("^que eu esteja na pagina principal do gmail\\.$")
    public void que_eu_esteja_na_pagina_principal_do_gmail() throws Throwable {
    
        driver.get("https://www.gmail.com");
        driver.findElement(By.xpath("//span[contains(text(),'Criar conta')]")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//div[contains(text(),'Para mim')]")).click();
        throw new PendingException();
      }
    
    @When("^criar uma nova conta com telefone invalido\\.$")
    public void criar_uma_nova_conta_com_telefone_invalido() throws Throwable {
            WebDriver driver = new ChromeDriver();  
    
         //Mapping the elements
         //no longer need to map
    
        // sending data to the application
    
        //when we call nomeusuario the first time, it is mapped.  When we call FakeName the first time, it is created
        nomeusuario.sendKeys(FakeName);
    
        throw new PendingException();
    }
    
    @Then("^o sistema não permite a finalização do cadastro\\.$")
    public void o_sistema_não_permite_a_finalização_do_cadastro() throws Throwable  {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
    
    
    ```


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2