I\'d like to capture the screenshot of the options that are displayed in the dropdown using selenium c# just like the image that is displayed below.
I\'ve t
Its really easy to do WITHOUT ANY ADDITIONAL TOOLS, believe me.
All you need is to do the following steps after click on Select element:
Create new Rectangle with position the same as have select and
var screenshotSize = lastOption.Position - Select.Position;
screenShotSize.height += lastOption.Size.Height;
Cut-off all except this Rectangle from the screenshot
In case of c# you can use for #6 the following code
Bitmap ScreenshotOfSelect = browserScreenshot.Clone(new Rectangle(point, size), screen.PixelFormat);
And as result you will receive bitmap that contains exactly only expanded Select =)
so as you see, its really-really easy to do =) And you don't need any tools except selenium. And also browser window can be invisible at the moment (as exmample in case of using phantomJS)
To open the dropdown you just have to .Click()
the element. So in your case,
IWebElement element = Driver.FindElement(By.Id("carsId"));
element.Click();
will expand the dropdown. The problem is that Selenium's screenshot functionality does not capture the open dropdown. You can get around that by using .NET to just take a screenshot of the active window. Working example code below.
static void Main(string[] args)
{
IWebDriver Driver = new FirefoxDriver();
Driver.Navigate().GoToUrl("http://www.tutorialspoint.com/html/html_select_tag.htm");
Driver.Manage().Window.Maximize();
IWebElement element = Driver.FindElement(By.Name("dropdown"));
element.Click();
TakeScreenShotOfWindow(@"C:\sshot.png");
}
// from http://stackoverflow.com/a/363008/2386774
public static void TakeScreenShotOfWindow(string filePath)
{
// Create a new bitmap.
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap.
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen.
bmpScreenshot.Save(filePath, ImageFormat.Png);
}
I don't think it'll be possible for normal drop downs. Since the overlay with the options you can choose from are displayed inside a native control and outside of the context of what selenium can work with. For this, you'll need some separate process or tool that can capture the screenshot of the desktop or application it self.
Link
Now, to capture the screenshot of desktop/application, we use Robot objects in Java.
For C#, you can use methods suggested in Capture screenshot of active window?.
Robot Sample Code:
try {
//Get the size of the screen stored in toolkit object
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
//Create Rectangle object using height and width of screen
//Here entire screen is captured, change it if you need particular area
Rectangle rect = new Rectangle(0, 0, d.width, d.height);
//Creates an image containing pixels read from the screen
Robot r = new Robot();
BufferedImage img = r.createScreenCapture(rect);
//Write the above generated buffered image to a file
File f = new File("myimage.jpg");
//Convert BufferedImage to a png/jpg image
ImageIO.write(img, "jpg", f);
} catch (Exception e) {
System.out.println(e.getMessage());
}
This will take the screenshot of the entire screen and save it into the file on given file location.
Selenium can only take screenshot of options in custom dropdowns made using Javascript/CSS and not in select dropdown.
Let me know if above code works or you need more help.