selenium - C#: How to create a folder for a specific function? -
i using selenium automation purpose. have class screenshot , page object method call element in different class every page. calling screenshot of page in every class. here think how create folder name screenshot in folder.
result.cs
class result { static int = 1; public static void screenshot() { itakesscreenshot screenshotdriver = mycollection.driver itakesscreenshot; screenshot screencapture = screenshotdriver.getscreenshot(); string path = @"..\..\..\results\screenshots\"; string timestamp = datetime.now.tostring("yy-mm-dd hh-mm-ss"); { screencapture.saveasfile(@path + + timestamp + ".png", system.drawing.imaging.imageformat.png); i++; } } }
loginpageobject.cs
[findsby(how = how.name, using = "txtusername")] public iwebelement username { get; set;} [findsby(how = how.name, using = "password")] public iwebelement pwd { get; set; } [findsby(how = how.classname, using = "login_button")] public void login(string uname, string paswd) { username.entertext(uname); pwd.entertext(paswd); clicklogin.click(); result.screenshot(); thread.sleep(4000); }
same homepageobject
main.cs
[test] public void initialize() { mycollection.driver = new twebdriver(); loginpageobject objlogin = new loginpageobject(); string pathfile = @"..\..\a.xlsx"; string sheetname = "common"; var excelfile = new excelqueryfactory(pathfile); var abc = in excelfile.worksheet(sheetname) select a; foreach (var in abc) { mycollection.driver.navigate().gotourl(a["url"]); } mycollection.driver.manage().window.maximize(); foreach (var in abc) { objlogin.login(a["uname"], a["paswd"]); } homepagepbject objhome = new homepageobject(); objhome.homefunction(); }
here main function initialize. how add screenshots folder. now, adding screenshot folder.
you can write it: create folter logs , after append screenshot folder inside it. if folder not exist, create it.
using nunit (but can same similar sintax in visualstudio.testtools):
public void savescreenshot(string screenshotfirstname) { var folderlocation = configurationmanager.appsettings["logpath"] +"\\screenshot\\"; if (!directory.exists(folderlocation)) directory.createdirectory(folderlocation); var screenshot = ((itakesscreenshot) _driver).getscreenshot(); var image = screenshottoimage(screenshot); var filename = new stringbuilder(folderlocation); filename.append(screenshotfirstname); filename.append(".png"); image.save(filename.tostring(), imageformat.png); } private static image screenshottoimage(screenshot screenshot) { image screenshotimage; using (var memstream = new memorystream(screenshot.asbytearray)) { screenshotimage = image.fromstream(memstream); } return screenshotimage; } [teardown] public static void cleanup() { browser.dispose(); var datetimenow = datetime.now; var data = datetimenow.tostring("dd/mm/yyyy hh:mm:ss"); integrationtest.writeinlog("test ends at: " + data); integrationtest.writeinlog("time execute: " + (datetimenow - inicioteste).totalseconds + " seconds"); var takescreenshoot = false; if (testcontext.currentcontext.result.outcome.equals(resultstate.failure)) { integrationtest.writeinlog("fails"); takescreenshoot = true; } else if(testcontext.currentcontext.result.outcome.equals(resultstate.error)) { integrationtest.writeinlog("error"); takescreenshoot = true; } else if(testcontext.currentcontext.result.outcome.equals(resultstate.setuperror)) { integrationtest.writeinlog("setup error"); takescreenshoot = true; } else if(testcontext.currentcontext.result.outcome.equals(resultstate.setupfailure)) { integrationtest.writeinlog("setup failure"); takescreenshoot = true; } else if(testcontext.currentcontext.result.outcome.equals(resultstate.inconclusive)) { integrationtest.writeinlog("inconclusive"); } else if (testcontext.currentcontext.result.outcome.equals(resultstate.success)) { integrationtest.writeinlog("sucess"); } else { integrationtest.writeinlog("unknow"); } if (takescreenshoot) { browser.savescreenshot(testcontext.currentcontext.test.name.toupper())); integrationtest.writeinlog("screenshot saved " + testcontext.currentcontext.test.name.toupper())); } integrationtest.writeinlog("\n"); }
Comments
Post a Comment