Home > Mobile >  How to call the string value to the void function
How to call the string value to the void function

Time:12-06

[Test]
        public void start()
        {
            string link = "https://www.w3schools.com/";
             //i want to call this link in below test case
        }

[Test]
        public void CC1()
        {
            IWebDriver driver = new ChromeDriver();
            WebDriverWait _wait = new WebDriverWait(driver, TimeSpan.FromSeconds(90));
            InitialLogin(driver, _wait);
            string url = start();//here i am calling this function
            driver.Navigate().GoToUrl(url);//here i want the above link
        }

the error message i am getting is cannot implicitly convert type 'void' to 'string'

CodePudding user response:

You're problem is that you have marked your start method as a test and given it a return type of void.

Changing the method as follows would solve your issue:

private string start()
{
    //I want to call this link in below test case
    return "https://www.w3schools.com/";
}

Better yet would be to use a ```const variable instead of a method to provide the string to your test method (the string is an unchanging constant so a method is not really needed). At the top of the class you could define the link as:

private const string StartUrl = "https://www.w3schools.com/"

Then in your test use it as follows:

[Test]
public void CC1()
{
    IWebDriver driver = new ChromeDriver();
    WebDriverWait _wait = new WebDriverWait(driver, TimeSpan.FromSeconds(90));
    InitialLogin(driver, _wait);
    driver.Navigate().GoToUrl(StartUrl);//here i want the above link
}

CodePudding user response:

You have to make your start function return the String.

public String start(){
  return "https://www.w3schools.com/";
}
  • Related