Is there a way to check if file exists using HandleOpenDialog

56 Views Asked by At

I have to click on "choose File" button and it opens the fileopen dialog. I need to check if file exists or not. Here is the code

public static void UploadFile(IWebDriver driver, string photoLocation, string photoName)
    {
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight)");
        IWebElement element = driver.FindElement(By.XPath("//*[text() ='Choose File']"));
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
        js.ExecuteScript("arguments[0].click();", element);           
        // using "HandleOpenDialog" to locate and upload file
        HandleOpenDialog hndOpen = new HandleOpenDialog();
        hndOpen.fileOpenDialog(photoLocation, photoName);
        Thread.Sleep(3000);
    }

This code works properly if the file exists in the location. if it doesn't exists, i want code to look someplace else. The test is stuck there for long time and fails the test.

Can someone help me here?

1

There are 1 best solutions below

1
sjanisz On

First of all it looks like HandleOpenDialog is some internal library class that You are using (not able to find it in search engine).

Regarding checking if file exists You can do that by using File.Exists static method (https://learn.microsoft.com/en-us/dotnet/api/system.io.file.exists?view=net-7.0).