扩展报告Selenium WebDriver截图加载问题c#

时间:2018-11-28 08:08:07

标签: c# screenshot relative-path selenium-extent-report

我正在使用以下代码将屏幕快照保存为与报告HTML文件相同的文件夹(d:\ DevTeam \ LSPTestSuites)。

SOAP-ERROR: Encoding: object has no 'cdsS_HEADER' property

但是如果我将文件夹复制到另一个位置,则不会加载屏幕截图。

保存在report.html文件中的完整屏幕快照文件路径如下:

import UIKit

var checklist = ["Item 1", "Item 2"]

class ChecklistViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource{

var newChecklistItemString: String?
var alertInputTextField: UITextField?

@IBOutlet weak var myTableView: UITableView!

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

var selectedChecklist: [String] = []

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return (checklist.count)
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
    cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
    cell.textLabel?.text = checklist[indexPath.row]

    if selectedChecklist.contains(checklist[indexPath.row]) {
        cell.accessoryType = .checkmark
    }
    else{
        cell.accessoryType = .none
    }

    return cell
}

// checkmarks when tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    selectedChecklist.append(checklist[indexPath.row])
    tableView.deselectRow(at: indexPath, animated: true)
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        let value = checklist.remove(at: indexPath.row)
        myTableView.reloadData()
    }
}

override func viewDidAppear(_ animated: Bool) {
    myTableView.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()

    addSlideMenuButton()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func addNewObject(_ sender: Any) {

    let alert = UIAlertController(title: "New Item", message: nil, preferredStyle: .alert)
    alert.addTextField { (alertInputTextField) in
        alertInputTextField.autocapitalizationType = .sentences
    }

    alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (action) in
        self.dismiss(animated: true, completion: nil)
    }))

    alert.addAction(UIAlertAction(title: "Add", style: .default, handler: { (action) in

        let textf = alert.textFields![0] as UITextField

        if(textf.text != "")
        {
            checklist.append(textf.text!)
        }
        self.myTableView.reloadData()

    }))

    self.present(alert, animated: true, completion: nil)

   }
}

2 个答案:

答案 0 :(得分:1)

一旦您在当前项目目录中有“ Screenshots”文件夹(位于根位置),则可以通过以下方式获取它:

    //Set Current Project directory
    public static String dir = AppDomain.CurrentDomain.BaseDirectory;
    public static FileInfo fileInfo = new FileInfo(dir);
    public static DirectoryInfo currentDir = fileInfo.Directory.Parent.Parent;
    public static string parentDirName = currentDir.FullName;

    //Save screenshot
    Screenshot file = ((ITakesScreenshot)driver).GetScreenshot();
    file.SaveAsFile(parentDirName + "\\Screenshots\\" + "Tempname.png", ScreenshotImageFormat.Png);

    //To load it for Selenium Extent Report
    testlog.Info("Details with screenshot" ,  MediaEntityBuilder.CreateScreenCaptureFromPath(parentDirName + "\\Screenshots\\" + "Tempname.png").Build());

答案 1 :(得分:0)

首先,我将屏幕快照保存到了报告目录(c:// temp / LSPTestSuites / screenshots)内的屏幕快照文件夹中,然后,我从屏幕快照文件路径中删除了报告目录路径,然后将其添加到报告。

ScreenshotFilePath = $"{Reporter.LatestResultsReportFolder}\\screenshots\\{screenshotName}.jpg";
ScreenshotFilePath = ScreenshotFilePath.Replace('/', ' ').Replace('"', ' ');
ss.SaveAsFile(ScreenshotFilePath, ScreenshotImageFormat.Png);
//to save relative screenshots in Reports html file - start
if(ScreenshotFilePath.IndexOf("screenshots") != -1)
{
       ScreenshotFilePath = ScreenshotFilePath.Substring(ScreenshotFilePath.IndexOf("screenshots"));
}
//to save relative screenshots in Reports html file - end
Logger.Trace($"ScreenshotFilePath => {ScreenshotFilePath}");

关键是在将屏幕截图添加到范围报告时使用相对路径(screenshots / screenshot.png):

CurrentTestCase.AddScreenCaptureFromPath(screenshotPath);

现在,我可以将reports文件夹移动到任何计算机的任何位置,并且屏幕快照的加载/显示没有任何问题。