简体   繁体   English

如何将 css 添加到 python 的 json2html 库中

[英]How to add css to json2html library in python

I ran my web automation script with the help of the python selenium and generated the out in JSON and converted the JSON output to HTML with the help of json2html convert. I ran my web automation script with the help of the python selenium and generated the out in JSON and converted the JSON output to HTML with the help of json2html convert.

Now I am trying to change the look of my json2html output but I am not sure how and where to add the CSS style to it.现在我正在尝试更改 json2html output 的外观,但我不确定如何以及在何处添加 CSS 样式。

I gave the table_attributes only the table changed rest tr , th didn't change.我只给了table_attributes表改变了 rest trth没有改变。

        # Initializing the counters
        header.setPASSCOUNT(0)
        header.setFAILCOUNT(0)
        header.setDISABLEDCOUNT(0)
        header.setENABLEDCOUNT(0)
        header.setERRMSG("NONE")
        header.setSTATUS("PASS")
        header.setCRASH("FALSE")
        header.setRESULT("next", "next", scenario_name, "next")
        header.setRESULT("TEST SCENARIO", scenario_name, scenario_name, "end")

        try:
            scenario_count = int(self.fetchValuefromJSON(self.scenario_obj[scenario_name],"scenario_count"))
        except:
            self.handleWrongJSON("Invalid scenario name")

        scenario_counter = 1
        while scenario_counter <= scenario_count:

            i = str(scenario_counter)
            current_scenario = self.fetchValuefromJSON(self.scenario_obj[scenario_name][i],"name")
            self.execute_scenario(i, current_scenario,scenario_name)
            if(header.STATUS == "PASS" or header.STATUS == "SUCCESS"):
                header.setPASSCOUNT(header.PASSCOUNT + 1)
            scenario_counter = scenario_counter + 1

        header.setRESULT("TESTCASES PASSED",header.PASSCOUNT,scenario_name,"end")
        header.setRESULT("TESTCASES FAILED", header.FAILCOUNT, scenario_name, "end")
        header.setRESULT("DISABLED COUNT", header.DISABLEDCOUNT, scenario_name, "end")
        header.setRESULT("TESTCASES FAILED", header.FAILCOUNT, scenario_name, "end")
        header.setRESULT("TOTAL TESTCASES EXECUTED", int(header.FAILCOUNT+header.PASSCOUNT), scenario_name, "end")

        if (header.PASSCOUNT == 0) & (header.FAILCOUNT == 0) & (header.DISABLEDCOUNT == 0):
            header.setRESULT("ERRMSG",header.ERRMSG,scenario_name,"end")

    print(header.RESULT)

    table_attributes = "style='width:100%;border: 1px solid #999;'"

    RESULT2 = json2html.convert(json=header.RESULT,table_attributes=table_attributes)

    #fname = header.getFOLDERPATH("logs/")+str(datetime.datetime.now())+"log"+scenariolist+releasestr+".html"
    fname = header.getFOLDERPATH("logs/") + str(
        datetime.datetime.now()) + "log" + scenariolist +".html"

    outfile = open(fname, 'w')
    outfile.write(RESULT2)
    outfile.close()

so can anyone please help me out Thank you in advance所以任何人都可以帮助我提前谢谢你

json2html only adds the style for your top-level element. json2html 只为您的顶级元素添加样式。 This is why your table had been applied to the style.这就是为什么您的表格已应用于样式。 If you want to add style to the tr or th , you should do another way.如果你想为trth添加样式,你应该做另一种方式。

  1. Use table_attributes to set id or class for your table.使用table_attributes为您的表设置idclass
  2. Use a separate css file to style your tr and th使用单独的css文件来设置您的trth

    table_attributes = "id='my-table'" RESULT2=json2html.convert(json=header.RESULT,table_attributes=table_attributes) table_attributes = "id='my-table'" RESULT2=json2html.convert(json=header.RESULT,table_attributes=table_attributes)

Your css rules您的 css 规则

#my-table tr {
  something here
}

#my-table th {
  something here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM