简体   繁体   中英

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.

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.

I gave the table_attributes only the table changed rest tr , th didn't change.

        # 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. 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.

  1. Use table_attributes to set id or class for your table.
  2. Use a separate css file to style your tr and th

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

Your css rules

#my-table tr {
  something here
}

#my-table th {
  something here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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