Java+Cucumber: get the current tag/abverb being executed - cucumber

I am trying to print the current step being executed in Cucumber. I am using a custom formatter to print the step definition. However, I also want to print the current adverb (Given, When, Then, And...) that is being executed. I might be missing something as well, is it possible in Cucumber? Here is my code:
Formatter:
public class MyCucumberFormatter implements ConcurrentEventListener {
#Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestStepStarted.class, runStartedHandler);
}
private EventHandler<TestStepStarted> runStartedHandler = new EventHandler<TestStepStarted>() {
#Override
public void receive(TestStepStarted event) {
startReport(event);
}
};
private void startReport(TestStepStarted event) {
if (!(event.testStep instanceof PickleStepTestStep)) {
return;
}
PickleStepTestStep testStep = (PickleStepTestStep) event.testStep;
log("Step: " + testStep.getStepText());
}
}
Example scenario:
Scenario: Test user life cycle: create user, activate and delete
Given A valid admin logs in
When Admin creates new user
And User is activated
Then User should successfully login
Right now, it prints as:
A valid admin logs in
Admin creates new user
User is activated
User should successfully login
I want it to print as:
Given A valid admin logs in
When Admin creates new user
And User is activated
Then User should successfully login

You can't do this in v4.x yet but you can do this in v5.0.0-RC1 by going through pickleStepTestStep.getStep().getKeyWord()

Related

Show different activities based on user choice in Android studio

How can I set my app to behave in such a way that when user arrives on my app for the first time a page appears for them to choose their status?
E.g
Student
Teacher
What I want is for two buttons to appear on that first screen they will see on the app.
So if a user clicks Student, each time he/she opens that app he will always be directed to an activity I designed for just students e.g student_activity layout.
But if the student choose Teacher, each time the app runs it will take the user to teacher_activity layout.
I will be so much thankful to anyone that can provide me with a good to set this on my app.
Plenty thanks as you look into it.
you need to use sharedPrefrences to store the user choice and then check the saved choice when your application is launched
create a SharedInfo class :
public class SharedInformation {
private Context context;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
public SharedInfo(Context c){
context = c;
sharedPreferences =context.getSharedPreferences("login.conf", Context.MODE_PRIVATE);
editor=sharedPreferences.edit();
}
public void setusertype(String type){
editor.putString("type",type);
editor.apply();
editor.commit();
}
public String getusertype(){
return sharedPreferences.getString("type","");
}
public void clearinfo(){
editor.clear();
editor.commit();
}
}
In your main/welcome activity:
SharedInfo sharedinfo = new SharedInfo(this);
if(sharedinfo.getusertype().isEmpty()){ //do nothing let the user pick a choice}
if(sharedinfo.getusertype().equals("student"){
//make an intent to send to student activity
}
if(sharedinfo.getusertype().equals("teacher"){
//make an intent to send to teacher activity
}
//etc
When the user picks a choice for the first time make sure to save it in your SharedInfo class like that :
sharedinfo.setusertype("yourtype");

Acumatica: Sign Out User After Process Completion

I am trying to sign out a user once a process is completed, I tried using the PXAccess or the PXAccessInfo classes in order to do so but did not manage to find a correct way in logging out a user. Are there any other means in signing out a user which I might have glossed over?
I adapted the standard SignOut code so it can be run from a graph extension instead of a Aspx.cs web page. It is equivalent to this SignOut menu item:
In this example I put the code in SOOrderEntry Initialize override so it signs out the current user as soon as you navigate to the SalesOrderEntry graph. You can put it in an Action event handler but I haven't tested it in a PXLongOperation context which runs in a separate thread context:
public class SOOrderEntry_Extension:PXGraphExtension<SOOrderEntry>
{
public override void Initialize()
{
System.Web.UI.Page page = System.Web.HttpContext.Current.Handler as System.Web.UI.Page;
if (page != null)
{
PX.Data.PXLogin.LogoutUser(PX.Data.PXAccess.GetUserName(), page.Session.SessionID);
PX.Common.PXContext.Session.SetString("UserLogin", string.Empty);
string absoluteLoginUrl = PX.Export.Authentication.AuthenticationManagerModule.Instance.SignOut();
page.Session.Abandon();
PX.Data.Auth.ExternalAuthHelper.SignOut(System.Web.HttpContext.Current, absoluteLoginUrl);
PX.Export.Authentication.FormsAuthenticationModule.
RedirectToLoginPage(PX.Data.Auth.ExternalAuthHelper.SILENT_LOGIN + "=None", true);
}
}
}

Gherkin Nested Steps meaning

I am writing Gherkin test cases and Java step definitions in my project. I am new to Gherkin and trying to understand the meaning of nested steps. Can you please help me to understand if the 2nd scenario given involves nested steps?
In my example, I would like to reuse 1st scenario code in 2nd scenario given statement logic. Is there a best way to reuse or rewrite the logic?
Note: Below example is written just to explain my question. It may not be a good Gherkin.
Background:
Given The application is opened
Scenario: Successful Login
Given the user name and password are entered
When login button is clicked
Then user login is successful
Scenario: Add Address Successful
Given user login is successful
And Add Address button is clicked
And user city, country are entered
when Submit button is clicked
Nested steps refer to calling defined steps inside a "main" one. In your example, the first scenario has the login functionality, which will / can be used in all other scenarios that require the user to be logged in.
So, the second scenario will have a Given step which calls the login action / steps of the first scenario. There are multiple ways to do this:
1. If you are defining those steps in the same class, it's just a matter of calling the same methods inside a different step / method.
Like so:
public class TestStepsOne {
// Steps from first scenario
#Given("^the user name and password are entered$")
public void enterUsernamePassword() throws Throwable {
System.out.println("User and password entered");
}
#When("^login button is clicked$")
public void clickLoginButton() throws Throwable {
System.out.println("Clicked login button");
}
#Then("^user login is successful$")
public void isLoggedIn() throws Throwable {
System.out.println("Logged in!");
}
// All together
#Given("the user is logged in")
public void loginSuccessfully() throws Throwable {
enterUsernamePassword();
clickLoginButton();
isLoggedIn();
}
}
Now you can use the Given the user is logged in in any scenario, and it will perform the login action.
2. Using Picocontainer -> details here
First you need to add these dependencies to your pom.xml:
<dependency>
<groupId>org.picocontainer</groupId>
<artifactId>picocontainer</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
</dependency>
You can separate your step definitions.
Like so:
public class TestStepsOne {
// Same as above, without the nested one
}
and second class:
public class TestStepsTwo {
private final TestStepsOne testStepsOne;
public TestStepsTwo(TestStepsOne testStepsOne) {
this.testStepsOne = testStepsOne;
}
#Given("the user is logged in")
public void loginSuccessfully() throws Throwable {
testStepsOne.enterUsernamePassword();
testStepsOne.clickLoginButton();
testStepsOne.isLoggedIn();
}
}
3. Using cuke4duke -> details here , includes examples
Like so:
public class CallingSteps extends Steps {
public CallingSteps(StepMother stepMother) {
super(stepMother);
}
#When("^I call another step$")
public void iCallAnotherStep() {
Given("the user is logged in"); // This will call a step defined somewhere else.
}
}
Hope this helps

setting bool value from a different class before navigating to that class

I'm trying to set a value to true after the user has been authenticated, so that they can use the page after authentication. When I set the value to true and redirect them to that same page that value is false again. I'm sure it has to do with different instances of the class but I dont know how to fix it.
This is the class that sets the value:
if (IsUserAuthorized())
{
Admin admin = new Admin
{
IsAuthorized = true
};
Response.Redirect("~/Admin.aspx");
}
else
{
LblErrorMessage.Text = "Please check your \"User Name\" or \"Password\" and try again.";
}
This is the class that needs to know the value:
public partial class Admin : System.Web.UI.Page
{
public bool IsAuthorized { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (IsAuthorized)
{//Do something} }
else
{
Response.Redirect("~/UserAuthentication.aspx");
}
}
classes do not persist between pages. What you need is either of the following two
Store the login status in cookies. That is how most sites do it. That is how e.g. on an email client you can navigate to various pages but still stay logged in.
Store the login status as session variables. Your login variable (true/false) resides in session.

Using vserv in J2ME Application , No ads shown

I'm trying to use Vserv ad network in my J2ME application(The Banner Ad one) but until now i can't receive any ads,i don't get any exception, i just notice that the vservAdFailed() method is always executed first, and the debug result is :
Ad Failed o=vInAppAdEngine.VservAd#e5125d64
This is the screen which must have the ad, i put all the code in it.
What is missing?!
public class Vserv extends Screen implements VservAdListener{
private VservManager vservManager;
private VservAd vservAd;
public Vserv(byte screenName,AppMidletBuilder app,AppData appData,Operation operation ){
super(screenName,app,appData.getLocalizationUtil(),appData.getImageUtil(),appData,operation);
//This is required only once in your application life cycle
Hashtable vservConfigTable=new Hashtable();
vservConfigTable.put("appId","My app Id");
vservManager=new VservManager(app,vservConfigTable);
}
protected void initScreen() {
//This is required for requesting new ad
vservAd=new VservAd(Vserv.this);
vservAd.requestAd();
}
protected void screenDefinition() {
}
public void vservAdReceived(Object obj) {
System.out.println("Ad Recieved");
if(((VservAd)obj).getAdType().equals(VservAd.AD_TYPE_IMAGE))
{
//Use retrived image ad for rendering
Image imageAd=(Image)((VservAd)obj).getAd();
} else if(((VservAd)obj).getAdType().equals(VservAd.AD_TYPE_TEXT))
{
//Use retrieved text ad for rendering
String textAd=(String)((VservAd)obj).getAd();
}
}
public void vservAdFailed(Object o) {
System.out.println("Ad Failed o="+o);
}
}
Have you replaced My app Id here
vservConfigTable.put("appId","My app Id");
Also the vservAdFailed() method is executed when no ads are available for your request.
You should have a button to handle the image rendered(Image Ad). Something like
imageItem = new ImageItem("", imageAd, ImageItem.LAYOUT_DEFAULT, "", Item.BUTTON);

Resources