Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

c# - Does selenium have a way to access a value that is generated after a specific action?

Lets say I press a button, and need to obtain the URL that gets generated with specific query parameters after that said button is pressed.

    public string pressAButton()
        {
            button.Click();
            var URL = driver.Url.ToString();
            return URL;
        }

I essentially need to create a URL with that button press and navigate to it to do some other stuff on specific days of the week.

Once I have this url I need another method to navigate to it but this new method has to be used for a separate test, and it would need the URL generated from the first method.

Would selenium have a way to handle this? The new test cannot call the pressAButton method in it, is there a way to obtain the value from the first method in a separate test?

question from:https://stackoverflow.com/questions/65896091/does-selenium-have-a-way-to-access-a-value-that-is-generated-after-a-specific-ac

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since you need to access a value from one test in another test, and the two tests are run in different test runs, you will need to save the URL to a text file. When the second test runs, open that text file, read the URL and use it.

Test #1

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));

button.Click();
wait.Until(ExpectedConditions.StalenessOf(button));

File.WriteAllText(@"pathourl.txt", driver.Url);

Note: driver.Url is already a string. No need to call driver.Url.ToString().

Test #2 (run some number of days later)

var urlFromPreviousTest = File.ReadAllText(@"pathourl.txt");

driver.Navigate().GoToUrl(urlFromPreviousTest);

Since you have two separate invocations of tests, you need some sort of persistent storage. Saving the URL to a file or database is your only option here. No in-memory solutions will work, because the existing memory allocated for a test is discarded once the test runner closes down.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...