I
I
itcry2019-04-28 18:37:51
C++ / C#
itcry, 2019-04-28 18:37:51

C# How to open a new tab in WebDriver from an async method?

I'm running some tests with a website using C# + Selenium. I run 4 tests at the same time in asynchronous mode:

Task t1 = Task.Run(() => Check1());
            Task t2 = Task.Run(() => Check2());
            Task t3 = Task.Run(() => Check3());
            Task t4 = Task.Run(() => Check3());
            await Task.WhenAll(t1, t2, t3, t4);

At the moment, in each of the methods Check1, Check2, etc, a separate browser is opened and the test takes place in it:
IWebDriver Browser1;
             ChromeDriverService service = ChromeDriverService.CreateDefaultService();
             service.HideCommandPromptWindow = true;
             ChromeOptions option = new ChromeOptions();
             option.AddArgument("--headless");
             Browser1 = new ChromeDriver(service, option);

After the end of the test, the browser is closed.
Everything works, everything is fine. In my program (WindowsForm) there is a field in which I enter some values, from which the test result can change. The problem is that now after each click of the "Check" button, I get 4 copies of the browser launched, running tests in them and closing the browser. This takes time.
I want to launch 4 copies of the browser when the program starts, and when I click on the "Check" button, I want to make sure that each test in its already open browser opens a new tab, runs tests in it and closes the tab. By this I want to reduce the time for the next tests if I change some input data.
Is there any example how best to do this?
Now if I declare a browser outside the asynchronous method, then this browser is not available to me in the asynchronous method and I cannot open a new tab for work in it.
Tell me, good people))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question