c# - Get chrome's console log -
i want build automation testing, have know errors appear in console of chrome.
there option error lines appear in console?
in order see console: right click somewhere in page, click "inspect element" , go "console".
i don't know c# here's java code job, hope can translate c#
import org.openqa.selenium.webdriver; import org.openqa.selenium.chrome.chromedriver; import org.openqa.selenium.logging.logentries; import org.openqa.selenium.logging.logentry; import org.openqa.selenium.logging.logtype; import org.openqa.selenium.logging.loggingpreferences; import org.openqa.selenium.remote.capabilitytype; import org.openqa.selenium.remote.desiredcapabilities; import org.testng.annotations.aftermethod; import org.testng.annotations.beforemethod; import org.testng.annotations.test; public class chromeconsolelogging { private webdriver driver; @beforemethod public void setup() { system.setproperty("webdriver.chrome.driver", "c:\\path\\to\\chromedriver.exe"); desiredcapabilities caps = desiredcapabilities.chrome(); loggingpreferences logprefs = new loggingpreferences(); logprefs.enable(logtype.browser, level.all); caps.setcapability(capabilitytype.logging_prefs, logprefs); driver = new chromedriver(caps); } @aftermethod public void teardown() { driver.quit(); } public void analyzelog() { logentries logentries = driver.manage().logs().get(logtype.browser); (logentry entry : logentries) { system.out.println(new date(entry.gettimestamp()) + " " + entry.getlevel() + " " + entry.getmessage()); //do useful data } } @test public void testmethod() { driver.get("http://mypage.com"); //do on page analyzelog(); } }
pay attention setup method in above code. use loggingpreferences object enable logging. there few types of logs, if want track console errors logtype.browser 1 should use. pass object desiredcapabilities , further chromedriver constructor , voila - have instance of chromedriver logging enabled.
after performing actions on page call analyzelog() method. here extract log , iterate through entries. here can put assertions or other reporting want.
my inspiration this code michael klepikov explains how extract performance logs chromedriver.
Comments
Post a Comment