How to take screenshot of every page in TESTNG.

52 views
Skip to first unread message

Kritika Gupta

unread,
Oct 30, 2017, 6:35:07 AM10/30/17
to testng-users
I have written a code to take screenshot and to store the screenshots based on the test case name.
I am calling this function before navigating to next page or before clicking on next button or submit or anyother button.

I am using hybrid structure and converting my project to TESTNG.

Is there any way or any annotation which will call screenshot function after each step in my method.

⇜Krishnan Mahadevan⇝

unread,
Oct 30, 2017, 7:12:41 AM10/30/17
to testng...@googlegroups.com
No. I dont think there is anything like that which you can use out of the box in TestNG.

Reason is because, TestNG test method is just a bunch of java statements bundled together with one or more assertions.

TestNG is aware of only the method, but what happens within the method is beyond TestNG's reach.

If you would like to just build this capability, you need to be leveraging something like an EventFiringWebDriver (or) perhaps resort to the command executor approach.

You can find more details about it in my blog post here : https://rationaleemotions.wordpress.com/2015/04/18/eavesdropping-into-webdriver/

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Kritika Gupta

unread,
Nov 1, 2017, 6:01:31 AM11/1/17
to testng-users
Thanks for your reply,

I am taking screenshot in every 10 sec and I want to implement this code with my main @Test using parallel testing. Also I am keeping screenshots based on testcase name folder. Test case name I am getting from data provider.

so finally I should be having 2 @Test , both needs dataprovider to get the testcasename.
Can you please guide me if my approach is Ok?
Or Is there anyway IF I can set the timer when to start the 2nd @Test so that it will take the screenshot only after login to the app?
Passing data provider to both @Test , will it cause running data provider twice.

Kindly advise.

Below is my code: 

@Parameters({"Environment"})

@Test(dataProvider = "returnTestcaseRow", priority = 1)

public static void main(String Environment, String flag, String TestCaseName)

{

try {

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");

EATradeCreation.CreationEATrade(Environment, flag, TestCaseName);

  } catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Test

public static void callScreen(String TestCaseName) throws Exception

{

while(true)

{

EAScreenshot.captureScreenShot(TestCaseName);

Thread.sleep(10000);

}

}

@DataProvider(name = "returnTestcaseRow")

public Object[][] EOT_testcasedata() throws Exception{

Object[][] testObjArray;

testObjArray = EOTdata.getTableArray(

"D:\\Automation\\EAAUTO\\src\\testData\\EOT_TestData_Updated.xlsx");

return testObjArray;



And this is my screenshot class which is taking screenshot

public class EAScreenshot {

public static void captureScreenShot(String TestCaseName) throws Exception {

Robot robot = new Robot();

BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

File dir = new File("D:\\TestResult\\"+ EOTMain.FOLDERNAME + "\\"+ TestCaseName);

dir.mkdir();

ImageIO.write(screenShot, "JPG", new File( dir + "\\"+System.currentTimeMillis()+".jpg"));

}

}






On Monday, October 30, 2017 at 4:42:41 PM UTC+5:30, Krishnan Mahadevan wrote:
No. I dont think there is anything like that which you can use out of the box in TestNG.

Reason is because, TestNG test method is just a bunch of java statements bundled together with one or more assertions.

TestNG is aware of only the method, but what happens within the method is beyond TestNG's reach.

If you would like to just build this capability, you need to be leveraging something like an EventFiringWebDriver (or) perhaps resort to the command executor approach.

You can find more details about it in my blog post here : https://rationaleemotions.wordpress.com/2015/04/18/eavesdropping-into-webdriver/

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

On Mon, Oct 30, 2017 at 4:05 PM, Kritika Gupta <kritika...@gmail.com> wrote:
I have written a code to take screenshot and to store the screenshots based on the test case name.
I am calling this function before navigating to next page or before clicking on next button or submit or anyother button.

I am using hybrid structure and converting my project to TESTNG.

Is there any way or any annotation which will call screenshot function after each step in my method.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

Krishnan Mahadevan

unread,
Nov 2, 2017, 12:24:17 AM11/2/17
to testng...@googlegroups.com

What do you intend to achieve with taking screenshots based on timers ? What is the usecase that you are trying to solve ?

 

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

Kritika Gupta

unread,
Nov 2, 2017, 12:52:01 AM11/2/17
to testng...@googlegroups.com
I want to take screenshots in every 10 secs only once my application is logged in , and want  to store the screenshots based on test case name. I am getting test case name from excel sheet using dataprovider.

So in my main testng class - there are 2 @test , 1 is to run the test case based on the testcase name and other is to take screenshot and both needs dataprovider parameter testcasename.

Is it possible to implement.





To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/qE-aOWTueSk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.



--
--
Kritika Gupta

Todd Bradley

unread,
Nov 2, 2017, 10:46:05 AM11/2/17
to testng...@googlegroups.com
Of course it's possible to implement - anything is possible in software - but it sounds like a terrible design. That's probably why Krishnan was asking what you're really trying to achieve. 

Do you run tests in parallel threads? If so, do you expect each thread to have a screen shot of whatever it's doing?

What do you want to happen if, when it's time for the screenshot to happen, TestNG is running a configuration method and not a test method? For example, what if it's executing a @BeforeMethod method?

What do you want to happen if a test method takes less than 10 seconds to execute? Is it OK to have no screenshot of that test?

The general approach to doing this is going to have nothing to do with TestNG, once again. You'll need to spawn off and manage a separate thread with a timer that takes the screenshot every 10 seconds, and your test will have to share the name of the currently executing test with that screenshot thread. How you do all that is a great topic for an intermediate Java training, not TestNG.


Cheers,
Todd.


Reply all
Reply to author
Forward
0 new messages