C# - 如何检测浏览器类型

时间:2016-11-10 23:57:27

标签: c# selenium browser detection

我正在使用C#运行Selenium,以便在多个浏览器(IE,FF,Chrome)上进行自动化测试,我的测试的一部分通过了Chrome而不是Firefox。

有没有办法检测自动化测试期间当前正在使用的浏览器类型?

2 个答案:

答案 0 :(得分:0)

使用以下代码

class Calculator
{
public:
    Calculator(
        std::unique_ptr<StepA> stepA, 
        std::unique_ptr<StepB> stepB,    
        std::unique_ptr<StepC> stepC
    )
    :m_stepA(std::move(stepA)),m_stepB(std::move(stepB)),m_stepC(std::move(stepC))
    {}

    FinalResult executeCalculation(const finalInputRequiredHere& input)
    {
        //logic
        auto stepAresult = stepA->perform(StepAParams);
        //logic
        auto stepBresult = stepB->perform(StepBParams);
        //logic
        auto stepCresult = stepC->perform(StepAParams);
        //logic
        return FinalResult();
    }

private:
    std::unique_ptr<StepA> m_stepA=nullptr;
    std::unique_ptr<StepB> m_stepB=nullptr;
    std::unique_ptr<StepC> m_stepC=nullptr;
};


void somewhereElse(){
    std::unique_ptr<StepA> stepa(new someStepAImpl());
    std::unique_ptr<StepB> stepa(new someStepBImpl());
    std::unique_ptr<StepC> stepa(new someStepCImpl());
    Calculator calc(
        std::move(stepa),
        std::move(stepb),
        std::move(stepc)
    );
    calc.executeCalculation(...);
}

答案 1 :(得分:0)

您可以从Nugget安装UAParser: https://www.nuget.org/packages/UAParser/

它将读取客户端标头并解析它。

例:

  //string uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3";

 // Request the header
  string uaString= HttpContext.Current.Request.UserAgent.ToString();

// get a parser with the embedded regex patterns
  var uaParser = Parser.GetDefault();

  // get a parser using externally supplied yaml definitions
  // var uaParser = Parser.FromYamlFile(pathToYamlFile);
  // var uaParser = Parser.FromYaml(yamlString);

  ClientInfo c = uaParser.Parse(uaString);

  Console.WriteLine(c.UserAgent.Family); // => "Mobile Safari"
  Console.WriteLine(c.UserAgent.Major);  // => "5"
  Console.WriteLine(c.UserAgent.Minor);  // => "1"

  Console.WriteLine(c.OS.Family);        // => "iOS"
  Console.WriteLine(c.OS.Major);         // => "5"
  Console.WriteLine(c.OS.Minor);         // => "1"

  Console.WriteLine(c.Device.Family);    // => "iPhone"