我正在使用Selenium进行自动化。我有一个截图和页面对象方法的类,可以为每个页面调用不同类中的所有元素。现在我打电话给每个班级的页面截图。但这里是思考如何创建文件夹名称获取该文件夹中的屏幕截图。
Result.cs
class Result
{
static int i = 1;
public static void screenshot()
{
ITakesScreenshot screenshotDriver = myCollection.driver as ITakesScreenshot;
Screenshot screenCapture = screenshotDriver.GetScreenshot();
string path = @"..\..\..\Results\ScreenShots\";
string timestamp = DateTime.Now.ToString("yy-MM-dd hh-mm-ss");
{
screenCapture.SaveAsFile(@path + i + 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);
}
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 = from a in excelFile.Worksheet(sheetName) select a;
foreach (var a in abc)
{
myCollection.driver.Navigate().GoToUrl(a["URL"]);
}
myCollection.driver.Manage().Window.Maximize();
foreach (var a in abc)
{
objLogin.Login(a["uname"], a["paswd"]);
}
HomePagePbject objHome = new HomePageObject();
objHome.HomeFunction();
}
这里我的主要功能是Initialize。那么现在如何将所有屏幕截图添加到该文件夹中。现在,我将它添加到屏幕截图文件夹。
答案 0 :(得分:1)
您可以编写类似的内容:创建一个日志文件夹,然后在其中附加一个屏幕截图文件夹。如果该文件夹不存在,请创建它。
使用NUnit(但您可以使用VisualStudio.TestTools中类似的sintax执行相同操作):
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 to 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 as " + TestContext.CurrentContext.Test.Name.ToUpper()));
}
IntegrationTest.WriteInLog("\n");
}