1

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();
}
0

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();
}


5
  • The field is not being filled when I use a different step. I want to use a single driver.findelement for all steps. Feb 18 '20 at 15:28
  • Thanks for bringing that up. I've modified it so that the field is being properly populated. Feb 18 '20 at 16:07
  • Sorry, but how do I do this in my test !. I didn't get it right !. I want to call the browser in one step, and with the browser open I want to enter the field information from the other step. Feb 18 '20 at 16:09
  • I've made some more changes, and included modified version of your original code in my answer. Feb 18 '20 at 16:38
  • Lyndon, Problem solved, forgive me you were right all the time !. We still needed to create the TestRunner.class file in java and add the Junit dependency in the POm file. Feb 26 '20 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.