Best JGiven code snippet using com.tngtech.jgiven.attachment.MediaType.MediaType
Source: ICanPrintTheThreadDumpTest.java
...31import com.tngtech.jgiven.annotation.ExpectedScenarioState;32import com.tngtech.jgiven.annotation.Hidden;33import com.tngtech.jgiven.annotation.ScenarioStage;34import com.tngtech.jgiven.attachment.Attachment;35import com.tngtech.jgiven.attachment.MediaType;36import com.tngtech.jgiven.junit.ScenarioTest;37import org.nuxeo.connect.tools.report.viewer.ICanPrintTheThreadDumpTest.Given;38import org.nuxeo.connect.tools.report.viewer.ICanPrintTheThreadDumpTest.When;39import org.nuxeo.connect.tools.report.viewer.ICanPrintTheThreadDumpTest.Then;40/**41 * Verify that we could print thread info serialized in JSON42 *43 * @since 8.444 */45@RunWith(FeaturesRunnerWithParms.class)46@Features(ReportFeature.class)47public class ICanPrintTheThreadDumpTest extends ScenarioTest<Given, When, Then> {48 public static class Given extends ICanReportTest.Given {49 }50 public static class When extends ICanReportTest.When {51 }52 public static class Then extends ICanReportTest.Then {53 }54 @ScenarioStage55 ThenICanPrint thenICanPrint;56 @Test57 public void i_can_print_the_thread_dump() throws IOException {58 // @formatter:off59 given()60 .the_report_to_run$name("mx-thread-dump").and()61 .the_report_component_is_installed().and()62 .the_report_is_registered();63 when()64 .i_run_the_report().and()65 .i_unmarshall();66 then()67 .the_report_is_a_mx_report()68 .and(thenICanPrint)69 .i_can_print_the_thread_dump()70 .end();71 // @formatter:on72 }73 public static class ThenICanPrint extends Stage<ThenICanPrint> {74 @ExpectedScenarioState75 ObjectNode report;76 @ExpectedScenarioState77 CurrentStep currentStep;78 public ThenICanPrint i_can_print_the_thread_dump() throws IOException {79 currentStep.addAttachment(80 Attachment.fromText(81 new ThreadDumpPrinter((ArrayNode) report.at(JsonPointer.compile("/value"))).print(new StringBuilder()).toString(),82 MediaType.PLAIN_TEXT_UTF_8));83 return self();84 }85 @ScenarioStage86 RuntimeSnapshotReport outerStage;87 @Hidden88 RuntimeSnapshotReport end() {89 return outerStage;90 }91 }92}...
...7import com.tngtech.jgiven.Stage;8import com.tngtech.jgiven.annotation.Quoted;9import com.tngtech.jgiven.annotation.ScenarioState;10import com.tngtech.jgiven.attachment.Attachment;11import com.tngtech.jgiven.attachment.MediaType;12import com.tngtech.jgiven.integration.android.AndroidJGivenTestRule;13import com.tngtech.jgiven.junit.SimpleScenarioTest;14import org.junit.Rule;15import org.junit.Test;16import org.junit.runner.RunWith;17import static android.support.test.espresso.Espresso.onView;18import static android.support.test.espresso.action.ViewActions.click;19import static android.support.test.espresso.assertion.ViewAssertions.matches;20import static android.support.test.espresso.matcher.ViewMatchers.withId;21import static android.support.test.espresso.matcher.ViewMatchers.withText;22@RunWith(AndroidJUnit4.class)23public class EspressoJGivenMainActivityTest extends24 SimpleScenarioTest<EspressoJGivenMainActivityTest.Steps> {25 @Rule26 @ScenarioState27 public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);28 @Rule29 public AndroidJGivenTestRule androidJGivenTestRule = new AndroidJGivenTestRule(getScenario());30 @Test31 public void clicking_ClickMe_changes_the_text() {32 given().the_initial_main_activity_is_shown()33 .with().text("AndroidTestingBox");34 when().clicking_the_Click_Me_button();35 then().text_$_is_shown("Text changed after button click");36 }37 public static class Steps extends Stage<Steps> {38 @ScenarioState39 CurrentStep currentStep;40 @ScenarioState41 ActivityTestRule<MainActivity> activityTestRule;42 public Steps the_initial_main_activity_is_shown() {43 // nothing to do, just for reporting44 return this;45 }46 public Steps clicking_the_Click_Me_button() {47 onView(withId(R.id.ActivityMain_Button)).perform(click());48 return this;49 }50 public Steps text(@Quoted String s) {51 return text_$_is_shown(s);52 }53 public Steps text_$_is_shown(@Quoted String s) {54 onView(withId(R.id.ActivityMain_TextView)).check(matches(withText(s)));55 takeScreenshot();56 return this;57 }58 private void takeScreenshot() {59 currentStep.addAttachment(60 Attachment.fromBinaryBytes(ScreenshotUtils.takeScreenshot(activityTestRule.getActivity()), MediaType.PNG)61 .showDirectly());62 }63 }64}...
Source: ThenSomeOutcome.java
...7import com.tngtech.jgiven.CurrentStep;8import com.tngtech.jgiven.Stage;9import com.tngtech.jgiven.annotation.*;10import com.tngtech.jgiven.attachment.Attachment;11import com.tngtech.jgiven.attachment.MediaType;12import org.junit.jupiter.api.Assertions;13import org.openqa.selenium.By;14import org.openqa.selenium.WebDriver;15public class ThenSomeOutcome extends Stage<ThenSomeOutcome> {16 public WebDriver webDriver;17 public ThenSomeOutcome()18 {19 webDriver= BaseTest.webDriver;20 }21 @ExpectedScenarioState(required = true)22 protected CurrentStep currentStep;23 @ExpectedScenarioState24 static String loggedInUserName;25 //@Rule26 //public ErrorCollector collector = new ErrorCollector();27 protected void AttachScreenShotToStage() {28 currentStep.addAttachment(Attachment.fromBase64(Helpers.CaptureScreenShot(webDriver), MediaType.PNG).withTitle("Screenshot"));29 }30 @Hidden31 public ThenSomeOutcome some_outcome() {32 return self();33 }34 public ThenSomeOutcome user_should_have_at_least_one_new_notification() {35 AttachScreenShotToStage();36 Assertions.assertTrue( HomePageMethods.GetUsersNotificationsNo()>0,"User has no notifications currently");37 return self();38 }39 @ExtendedDescription("This should be the current logged in/impersonated as username")40 public ThenSomeOutcome current_task_should_have_a_correct_assigned_to_value() {41 Helpers.WaitTillElementExist(webDriver,10, By.cssSelector("div.card-row>div.card-col-field>div>div>span"));42 AttachScreenShotToStage();43 Assertions.assertEquals(loggedInUserName, WorkFlowTasksPageMethods.GetTaskAssignee(),"Task assignee is not "+loggedInUserName);44 return self();45 }46 public ThenSomeOutcome task_state_should_be(String expectedState)47 {48 AttachScreenShotToStage();49 Assertions.assertEquals(expectedState, WorkFlowTasksPageMethods.GetTaskState(),"Task state is not "+expectedState);50 return self();51 }52 public ThenSomeOutcome MySubmissionsAreMoreThan(String email, @Hidden int count) {53 int MyActualSubmissionsCount = BasePageMethods.GetMySubmissionsCount();54 //AttachScreenShotToStage();55 currentStep.addAttachment(Attachment.fromText("Submissions count for user "+email+" is "+MyActualSubmissionsCount, MediaType.PLAIN_TEXT_UTF_8));56 Assertions.assertTrue(MyActualSubmissionsCount>count,"My submissions count is "+MyActualSubmissionsCount);57 return self();58 }59}...
Check out the latest blogs from LambdaTest on this topic:
Having a good web design can empower business and make your brand stand out. According to a survey by Top Design Firms, 50% of users believe that website design is crucial to an organization’s overall brand. Therefore, businesses should prioritize website design to meet customer expectations and build their brand identity. Your website is the face of your business, so it’s important that it’s updated regularly as per the current web design trends.
Collecting and examining data from multiple sources can be a tedious process. The digital world is constantly evolving. To stay competitive in this fast-paced environment, businesses must frequently test their products and services. While it’s easy to collect raw data from multiple sources, it’s far more complex to interpret it properly.
Traditional software testers must step up if they want to remain relevant in the Agile environment. Agile will most probably continue to be the leading form of the software development process in the coming years.
Are members of agile teams different from members of other teams? Both yes and no. Yes, because some of the behaviors we observe in agile teams are more distinct than in non-agile teams. And no, because we are talking about individuals!
Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!