Pytest-bdd giving an name error for variables defined in scenario outline - pytest-bdd

I am trying to run a test with ptest-bdd. I've used the scenario outline but the variable username is returning a name error saying that username is not defined. I would greatly appreciate a second pair of eyes on my code.
Here is the feature file:
#login
Feature: Valid Login
As a employee,
I want to login to my account
So I can access the application
Background:
Given the login page is displayed
Scenario Outline: Manager enters correct login credentials and Ok button
When the user enters their <username> and <password> correctly
And the user clicks OK
Then the page redirects to the manager home page
Examples: Manager
| username | password |
| fake_name | 1234 |
And here is the test_login_steps.py code:
from pytest_bdd import scenarios, scenario, given, when, then, parsers
from pages.signin import QaSigninPage
from pages.managerHomepage import QaManagerHomepage
import pytest
CONVERTERS = {
'username': str,
'password': str,
}
scenarios('../features/login.feature', example_converters=CONVERTERS)
# Create Page Objects
#pytest.fixture(scope='session')
def pages(test_manager):
pages={"signin_PAGE": QaSigninPage(test_manager.browser),
"manager_home_PAGE": QaManagerHomepage(test_manager.browser)}
return pages
# Given Steps
#given('the login page is displayed')
def login_visit(pages):
pages["signin_PAGE"].load()
#when('the user enters their <username> and <password> correctly')
def enter_credentials(pages):
pages["signin_PAGE"].enter_username(username)
pages["signin_PAGE"].enter_password(password)

I forgot to pass the variables into the function... I figured it out.

When you are using CONVERTERS use scenario in place of scenarios, which allows you to pass examples as parameters

Related

How to run one Cucumber (With Protractor) scenario over multiples files?

I have a Cucumber feature like this one:
#MainSuite
Scenario: Verify that user can login
Given I can see the login form
Then I set a username
And I set a password
And I click in Login button
Then I see the "wrong-password" message
I need to check that the user can login in 5 different pages.
I need to run that feature in 5 different places.
It's like, I need to run that feature in /login.html, /old_login.html, /after_restore_password.html and more (it's just an example).
Do you know how to do it?
Currently, I have only one file hardcoded. Obviously I need to change it.
this.Given(/^I can see the login form$/, function(done) {
this.goTo('login.html');
browser.wait(EC.visibilityOf(this.loginFormContainer));
done();
});
Create an object containing the different login pages that you can go to.
Include an if statement in your step def if there is any extra code that needs to be executed.
this.Given(/^I can see the "(.*)" login form$/, function(loginPage, done) {
var logins = {
login : "login.html",
afterpasswordreset: "after-password-reset.html"
}
loginPage = loginPage.toLowerCase().replace(" ", "");
this.goTo(logins[loginPage]);
browser.wait(EC.visibilityOf(this.loginFormContainer));
done();
});
Make either separate Scenarios or feature file for each variation of login, or simply create a Scenario Outline for them
EDIT
Here is how I would implement the Scenario Outline:
#MainSuite
Scenario Outline: Verify that user can login
Given I can see the "<loginType>" login form
And I set a username
And I set a password
When I click in Login button
Then I see the "wrong-password" message
Examples:
| loginType |
| Login |
| After Password Reset |

Getting user keycloak Not Found exception

I can't get user groups like in samples.
Samples from:
Take a look at our testsuite. For example:
UserTest
GroupTest
Sample code from examples for receiving groups user is member of:
List<GroupRepresentation> membership = realm.users().get(user.getId()).groups();
My approach:
1. I create keycloak object for admin-cli client in myrealm realm:
this.keycloak = KeycloakBuilder.builder()
.serverUrl("http://localhost:18080/auth")
.realm("myrealm")
.username("admin")
.password("admin")
.clientId("admin-cli")
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
.build();
When I try to get user:
//this line works
final UserResource userr = this.keycloak.realms().realm("myrealm").users().get("admin");
//this two doesnt, in both result is javax.ws.rs.NotFoundException: HTTP 404 Not Found
final UserRepresentation ur = userr.toRepresentation();
final List<GroupRepresentation> groups = this.getRealm().users().get(user.getId()).groups();
In keycloak from admin-cli I created realm "myrealm" with 2 users and 2 groups
Every user is member of both groups. admin is one of this users and is member of this two groups.
Users I've created are in "myrealm" realm, "admin" is one of them.
I've olso tried to give all available roles from clients and realm but this changes nothing.
admin-cli I meant keycloak app on localhost
What am I missing?
Libs I am using:
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.admin.client.resource.UsersResource;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
I just stumbled into the same problem as you. The problem was with the get method. Its argument is not a user name (admin), but a user identifier. Something like 494061c1-c8f3-44c9-8542-df895af81716 .
In my case I properly tried to pass the user ID, but I used token ID instead which is something completely different.

Get the current logged in user in the main template

I have several templates in my play/scala project.
I want to get access to the currently logged in user in my main template.
(so that I can put a Welcome 'username' text on the top of the page on all templates not only the "logged in pages")
I could put a parameter in my main template:
#(user: User)
But I have several templates that reference to the main template that doesn't have access to the logged in user. Pages like about, contact etc.
The only templates that have access to the logged in user is the "logged in" pages. Like user profile, user settings. From there, I can pass the logged in User object to the main template. But from the other pages I can't.
If I make the parameter "user" in main template as an optional parameter it will be problems anyway since I'm going to use the logged in users data on all pages if a user is logged in.
How could I solve this?
Please tell me if you need more input.
You would need to use session to store id which you can use to identify the user, for example use user email as an id, from any action in controller retrieve user from db base on this id stored in session and pass 'username' as an Option to template, use option in case there is no logged in user, like this:
def about = Action { implicit request =>
val username:Option[String] = User.findUserName(request.session.get(userSession))
Ok(views.html.Applications.about(username)
}
example of about template, just pass username to main template
#(username:Option[String])
#main(username) {
// your content
}
example of main template display username if it is defined
#(username: Option[String])(content: Html)
#if(username.isDefined) {
<p> username.get</p>
}
If I understand you correctly, you need this:
Controller (Application.scala):
object Application extends Controller {
def index = Action {
val user = "smth" //assign your user name here
val parameter = "another parameter"
Ok(views.html.yourTemplate(user, parameter))
}
}
Template (yourTemplate.scala.html):
#(user: String, parameter: String)
<!--main template wrapping yourTemplate-->
#main("Welcome") {
<p>Hello, #user</p>
}

Bootstrap a grails app with dummy data when spring security facebook is involved

I've created a grails app that uses spring security to allow a user to authenticate via facebook, and I can successfully print out the facebook username onto one of the views, so thus far I don't have any issues.
My problem lies when trying to bootstrap my application with some sample data for my given facebook user, so I don't have to enter it every time the application starts up.
This is how I'm trying to bootstrap my own facebook account, I have the following in Bootstrap.groovy :
def adminRole = new AppRole(authority: 'ROLE_ADMIN').save(flush: true)
def userRole = new AppRole(authority: 'ROLE_USER').save(flush: true)
def testUser = new AppUser(username: 'facebook_563645402', enabled: true,
password: 'my-hashed-pw-here',
surveys: [jamies])
testUser.save(flush: true)
AppUserAppRole.create testUser, adminRole, true
For the record, I've added a hasMany for the surveys field mentioned above onto AppUser.
When I fire up the app and try to connect, I get the following error :
URI
/web/j_spring_security_facebook_check
Class
grails.validation.ValidationException
Message
Validation Error(s) occurred during save(): - Field error in object 'web.AppUser' on field 'username': rejected value [facebook_563645402]; codes [web.AppUser.username.unique.error.web.AppUser.username,web.AppUser.username.unique.error.username,web.AppUser.username.unique.error.java.lang.String,web.AppUser.username.unique.error,appUser.username.unique.error.web.AppUser.username,appUser.username.unique.error.username,appUser.username.unique.error.java.lang.String,appUser.username.unique.error,web.AppUser.username.unique.web.AppUser.username,web.AppUser.username.unique.username,web.AppUser.username.unique.java.lang.String,web.AppUser.username.unique,appUser.username.unique.web.AppUser.username,appUser.username.unique.username,appUser.username.unique.java.lang.String,appUser.username.unique,unique.web.AppUser.username,unique.username,unique.java.lang.String,unique]; arguments [username,class web.AppUser,facebook_563645402]; default message [Property [{0}] of class [{1}] with value [{2}] must be unique]
Which appears to complain about the username not being unique.
If by trying to bootstrap some data breaks the unique constraints on the facebook username, how can I possibly ever pre define any data for a user?
A quick Googling brings up a few suggestions (link1, Grails spring security bootstrap, but so far they haven't helped, any ideas?
EDIT:
Delving deeper into the error that grails reports, I can see that the root of the above error is located in DefaultFacebookAuthDao, line 135, which mentions the following :
AppUserDomainClazz.withTransaction {
appUser.save(flush: true, failOnError: true)
}
So, by authenticating, spring security attempts to save a user domain object...
EDIT 2 :
This is my Bootstrap.groovy
def testUser = new AppUser(username: 'facebook_563645402', enabled: true,
password: 'my-hashed-pw', surveys: [new Survey(1)])
testUser.save()
def fbUser = new FacebookUser(uid: 563645402)
fbUser.save(flush: true)
Both FacebookUser and AppUser were generated via the spring security facebook quickstart, with the only change being to add static hasMany = [surveys: Survey] to AppUser.
It looks like the data has already been predefined, otherwise there wouldn't be a unique constraint violation. Just check for the existence of the data and only create it if needed:
def adminRole = AppRole.findOrSaveByAuthority('ROLE_ADMIN')
def userRole = AppRole.findOrSaveByAuthority('ROLE_USER')
String username = 'facebook_563645402'
if (!AppUser.findByUsername(username)) {
def testUser = new AppUser(username: username, enabled: true,
password: 'my-hashed-pw-here')
testUser.addToSurveys(jamies)
testUser.save()
AppUserAppRole.create testUser, adminRole, true
}
Spring Security Facebook tries to create a new user with same username (facebook_563645402). Because it cannot find it by uid.
As I see from you code, you just create an user, with filled username, but it's not a facebook user, and uid field isn't filled. So, the plugin cannot find any facebook user and tries to create a new one, using 'facebook_563645402' username by default.
There two ways: or change username for user created in Bootstrap (if it's not a facebook user), or create a Facebook User also (will be used for authentication).

Automatic mediawiki import with powershell_script

I found a nice script to import xml using powershell
http://slash4.de/tutorials/Automatic_mediawiki_page_import_powershell_script
Currently I don't get them run. I'm sure, this is a problem with the permissons.
First I set the wiki to allow anybody to upload an import
$wgGroupPermissions['*']['import'] = true;
$wgGroupPermissions['*']['importupload'] = true;
Then I get this error: Import failed: Loss of session data.
I try to figure out to pass the user and password to this line in powershell
$req.Credentials = [System.Net.CredentialCache]::DefaultCredentials
and changed it to
$req.Credentials = [System.Net.CredentialCache]::("user", "pass")
Import failed: Loss of session data. Again?
How can I pass the user/password to the website?
The Loss of session data error is generated when the edit token sent with the request does not have the expected value.
In the script you linked to, the $wikiURL string contains editToken=12345. That does not look like a valid MediaWiki edit token, so it's not surprising that it will fail.
In current versions of MediaWiki, the edit token for non-logged-in users is always +\. You could try replacing 12345 in the script with that (or, rather, with its URL-encoded version %2B%5C) and see if it helps.