Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoking IApp.Dispose() does not terminate the webdriver #87

Open
jeromelaban opened this issue Feb 21, 2024 · 6 comments
Open

Invoking IApp.Dispose() does not terminate the webdriver #87

jeromelaban opened this issue Feb 21, 2024 · 6 comments

Comments

@jeromelaban
Copy link
Member

Invoking:

does not terminate the web driver, which can cause issues in multi-build environments.

@jeromelaban
Copy link
Member Author

@ADD-David-Antolin we've been trying to reproduce the issue, but we're unable to when calling App.Dispose(). At this time, the driver is properly closed.

Have you tried invoking this method in the assembly tear down, or test tear down?

@ADD-David-Antolin
Copy link

ADD-David-Antolin commented Feb 28, 2024

@jeromelaban In TestBase.cs

[Setup]
public void SetupTest()
{
    this.App = AppInitializer.AttachToApp();
}

[TearDown]
public void TearDownTest()
{
    try
    {
        this.TakeScreenShot("teardown");
    }
    finally
    {
        this.App.Dispose();
    }
}

I assume that is test tear down.

@jeromelaban
Copy link
Member Author

@ADD-David-Antolin yes, this is the case. Then if the driver stays active, it may be a bug in selenium or the driver itself. You may need to find a way to determine the PID of the process being launched based on the PTY being used by the build scripts.

Something like this could do, to terminate those processes:

ps -o pid,tty,cmd -ax | grep $(tty) | grep 'msedgedriver' | awk '{print $1}' | xargs kill

@ADD-David-Antolin
Copy link

ADD-David-Antolin commented Mar 6, 2024

@jeromelaban The agent machines are Windows server so it does not have a tty equivalent I think. Do you know the equivalent of what snippet but for PowerShell?

@jeromelaban
Copy link
Member Author

@ADD-David-Antolin indeed, this technique does not work for Windows. Aside from fixing Selenium, something that is out of our hands at this point. Browsing the source from Selenium may explain why this happens (somewhere in there)

@ADD-David-Antolin
Copy link

@jeromelaban I workarounded it in code:

private static void KillMsEdgeDriverProcessWorkaround(IApp app)
{
    if (AppInitializer.GetLocalPlatform() == Platform.Browser
        && typeof(SeleniumApp).GetField("_driver", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(app) is EdgeDriver edgeDriver)
    {
        Dictionary<string, object> msedgeEntry = (Dictionary<string, object>)edgeDriver.Capabilities["msedge"];

        string userDataDir = (string)msedgeEntry["userDataDir"];

        string msedgedriverpid = userDataDir[(userDataDir.IndexOf("scoped_dir") + "scoped_dir".Length)..].Split("_")[0];

        app.Dispose();

        Process msedgeDriverProcess = Process.GetProcessById(Convert.ToInt32(msedgedriverpid, CultureInfo.InvariantCulture));

        if (msedgeDriverProcess.ProcessName.Contains("msedgedriver", StringComparison.OrdinalIgnoreCase))
        {
            msedgeDriverProcess.Kill();
        }
    }
}

I tested that this works in Windows machines.

Maybe you can add this somehow to your codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants