How to dynamize cucumber value and pass that into the login page - angularjs

How to dynamize cucumber value and pass that into the login page, for example:
If I use Adam in the cucumber scenario then it should automatically use adam login details and other information and if i use another name it should use that person's information. So i do not have to manually enter it in every step defination. How Can i achieve it?
Scenario: Add an item to shopping bag to place the order using mastercard
Given that "Adam" is logged in to his account
When he searches and adds an item from "men" section to his shopping bag
Then he can place the order
export class LoginUser implements Task {
static called(name: string): LoginUser {
return new LoginUser(name);
}
performAs(actor: PerformsTasks): PromiseLike<void> {
return actor.attemptsTo(
Click.on(homePageElementsMap.lnk_login),
Enter.theValue('test#test.com').into(loginPageElementsMap.txt_login_email),
Enter.theValue('password111').into(loginPageElementsMap.txt_login_pwd),
Click.on(loginPageElementsMap.btn_login)
);
}
constructor(private name: string) {
}
}

disclaimer: this is a java answer, I misread the tag. I am sure you can something similar for js.
For our tests we have a variety of users as well and we use a java class with userdata for this, set up as maps.
We pass the value to a static method public Map<String, String> getUserData(String username) which contains a switch for the variety of users we have, each user is given a map of data we want to use in the tests. The users each have a private Map in the class containing login information.
Of course, there are more scalable ways, this one just gives us the flexibility of a single public getUserData(userName) and behind this we can do with the storage whatever we want.
Short answer: static java code containing user information for a variety of usernames.

Is this the only way to do it? - http://docs.behat.org/en/v2.5/guides/1.gherkin.html

Related

In Selenium, is it good to reuse same instance of the Page class multiple times in the same test method or should I create new instance?

I need to test the website: https://www.rediff.com using PageFactory pattern.
My test method does the following steps:
Step-1. Goto Rediff home page https://www.rediff.com/
Step-2. Click on Sign-in button
Step-3. Enter the username and password and click on Submit button
Step-4. Click on rediff.com link at the extreme top left which will take the user back to Rediff home page. Now enter the text in the search bar and click on Search button
Below is the test class:
public class RediffLoginTest extends BaseClass
{
#Test
public void loginAndSearch() throws InterruptedException
{
// Step-1
getDriver().navigate().to("https://www.rediff.com");
// Step-2
RediffHomePage rhp = new RediffHomePage();
PageFactory.initElements(getDriver(), rhp);
rhp.signIn().click();
// Step-3
RediffLoginPage rlp = new RediffLoginPage();
PageFactory.initElements(getDriver(), rlp);
rlp.enterEmailID().sendKeys("hello");
rlp.enterPassword().sendKeys("demopass");
rlp.submitLogin().submit();
// Step-4
rlp.goToHomePage().click();
rhp.enterSearchText().sendKeys("demo");
rhp.submitSearch().click();
}
}
As per the Step-4 explained above, it takes me back to Rediff home page. I have already created an object rhp of RediffHomePage in Step-2. So, my question is, for the Step-4, is it good to reuse this same object rhp as shown in the above code or should I create another object of the same page class in the same test method for e.g. say rhp1 of RediffHomePage as shown in the below code ?
RediffHomePage rhp1 = new RediffHomePage();
PageFactory.initElements(getDriver(), rhp1);
rhp1.enterSearchText().sendKeys("demo");
rhp1.submitSearch().click();
If possibly, in such situations you definitely should reuse the existing objects, not to create again and again new redundant objects.

Use two completely different classes as one

I have very stupid question about design patterns: let's say we have two classes Post and Product, for each of them we have different table in the DB, and they have nothing in common with each other, so we can't create base class for them. Some Posts even contains Products. And here's what we should do with them:
Somehow store Post and Product instances in the DB, pack them in one array(using C++, if it matters) when user requests news feed from the next item, send it to the client, and receive and unpack on the client side(using Java).
Next, we have to show both Post and Product in the one list(such as news feed on the Facebook).
Also, we can share Post or Product with our friends using chat. So we can send Post or Product as an attachment of the message(consequently, we should to store id of sent Post or Product in the column attached_item of the messages table in the DB on the server side).
So, what design pattern would be best here? How should I implement the Post and Product classes?
It is a very broad question, but here is a skeleton of what you could you, just to give you some ideas:
// An interface containing methods specific to objects you can list
interface Listable {}
// An interface containing methods specific to objects you can share
interface Shareable {}
// An interface containing methods specific to objects you can send
interface Sendable {}
class Post implements Listable, Shareable, Sendable {
List<Product> products;
}
class Product implements Listable, Shareable, Sendable {
}
class ListManager {
public void addToList(Listable element) { }
}
class ShareManager {
public void share(Shareable element) { }
}
class SendManager {
public void send(Sendable element) { }
}
You could then use Post and Product interchangeably this way:
Post post = new Post();
Product product = new Product();
ListManager listManager = new ListManager();
listManager.addToList(post);
listManager.addToList(product);
ShareManager shareManager = new ShareManager();
shareManager.share(post);
shareManager.share(product);
SendManager sendManager = new SendManager();
sendManager.send(post);
sendManager.send(product);
Regarding the database representation, as suggested fusiled in his comment, just stick them in 2 separate tables. With a mapping table in between to link the products to their post.
EDIT
Regarding the issue with the MESSAGES table
You could add a new mapping table MESSAGE_ATTACHED_ITEM with columns messageId, postId, productId. Only set a value to the relevant colum when attaching an item to a message
Or an other option would be to have an ATTACHED_ITEM table with an id only.
And have Post and Product tables to have a foreign key to this table Id.
you can then stick this attachedItemId into your attached_item column
I think the solution could be simpler than you think. Why don't you ust use a common Java-like interface and hide the implementation details?
Just implement a common interface with the methods you need. Supposing this common interface is called EntityInterface:
public class Post implements EntityInterface {};
public class Product implements EntityInterface {};
Then when you want to handle these classes, you treat them as an EntityInterface object:
EntityInterface myNewPost = new Post();
EntityInterface myNewProduct = new Product();
//Now you see myNewProduct and myNewPost as EntityInterface objects
These code fragments are in Java, but use virtual functions in C++ and you get the same.

Passing indefinite Query Parameters with RESTful URL and reading them in RESTEasy

I have a requirement to design a RESTful Service using RESTEasy. Clients can call this common service with any number of Query Parameters they would want to. My REST code should be able to read these Query Params in some way. For example if I have a book search service, clients can make the following calls.
http://domain.com/context/rest/books/searchBook?bookName=someBookName
http://domain.com/context/rest/books/searchBook?authorName=someAuthor& pubName=somePublisher
http://domain.com/context/rest/books/searchBook?isbn=213243
http://domain.com/context/rest/books/searchBook?authorName=someAuthor
I have to write a service class like below to handle this.
#Path("/books")
public class BookRestService{
// this is what I currently have, I want to change this method to in-take all the
// dynamic parameters that can come
#GET
#Path("/searchBook")
public Response searchBook(#QueryParam("bookName") String bookName,#QueryParam("isbn") String isbn) {
// fetch all such params
// create a search array and pass to backend
}
#POST
#Path("/addBook")
public Response addBook(......) {
//....
}
}
Sorry for the bad format (I couldn't get how code formatting works in this editor!). As you can see, I need to change the method searchBook() so that it will take any number of query parameters.
I saw a similar post here, but couldn't find the right solution.
How to design a RESTful URL for search with optional parameters?
Could any one throw some light on this please?
The best thing to do in this case would be using a DTO containing all the fields of your search criteria. For example, you mentioned 4 distinct parameters.
Book Name (bookName)
Author Name (authorName)
Publisher Name (pubName)
ISBN (isbn)
Create a DTO containing the fields having the following annotations for every property you want to map the parameters to:
public class CriteriaDTO{
#QueryParam("isbn")
private String isbn;
.
.
Other getter and setters of other properties
}
Here is a method doing that for your reference:
#GET
#Produces("application/json")
#Path("/searchBooks")
public ResultDTO search(#Form CriteriaDTO dto){
}
using following URL will populate the CriteriaDTO's property isbn automatically:
your.server.ip:port/URL/Mapping/searchBooks?isbn=123456789&pubName=testing
A similar question was asked here: How do you map multiple query parameters to the fields of a bean on Jersey GET request?
I went with kensen john's answer (UriInfo) instead. It allowed to just iterate through a set to check which parameters were passed.

Different data sets per subdomain in Cakephp?

I am writing a personal bookmarks application and am looking for a way to have totally different content between subdomains.
I have written the application and it works fine, but have yet to implement multiple collections. I have the following models:
Tag (unused so far)
Bookmark
Subject
Bookmarks are categorized in subjects and I'm planning to allow tagging in the future and see if that helps me manage my bookmarks more easily.
The current problem I have is that I'd like to separate bookmarks as a whole. I want to use subdomains like webdevelopment.bookmarks.local, languages.bookmarks.local, linux.bookmarks.local that work with an entire own set of domains and bookmarks.
I am considering adding a new model called Set (short for "bookmark sets") and defining sets based on the subdomain.
According to that plan I'd have to rewrite all $this->...->find-queries in the entire App to contain the condition "set_id" = $SubdomainBasedSetid".
While it wouldn't be that much work, I was wondering if it could be done smarter, maybe that Cake would only see the relevant bookmark set per subdomain.
well, your solution is right. But instead of using subdomains, you can use prefix, so you don't have check and set yourself. Since you use subdomains, I assume that these sets are fixed or rarely change. So you don't really need a sets table, just use the set name directly in your bookmark record, so you don't have to convert between name and id.
According to that plan I'd have to rewrite all $this->...->find-queries in the entire App to contain the condition "set_id" = $SubdomainBasedSetid".
As all models extend AppModel, it is possible to edit all queries there before they happen (ie. DRY). :)
// app/app_model.php
class AppModel extends Model {
public function beforeFind($queryData) { // old query
// make changes
return $queryData; // new query
}
However, if you don't want this functionality for all models (or even if you do for now), a better place might be a behavior as this allows you to pick and choose where and when it is loaded:
// app/models/behaviors/subdomain.php
class SubdomainBehavior extends ModelBehavior {
protected $_defaults = array('field' => 'Site.subdomain');
public function setup(&$model, $config = array()) {
$this->settings[$model->alias] = array_merge($this->_defaults, $config);
}
public function beforeFind(&$model, $queryData) {
$domain = $_SERVER['SERVER_NAME'];
$subdomain = substr($domain, 0, strpos($domain, "."));
$queryData['conditions'][$this->settings['field']] = $subdomain;
return $queryData;
}
}
// app/app_model.php
class AppModel extends Model {
$actsAs = array('Subdomain' => array('field' => 'Set.slug'));
}
Tag (unused so far)
To save reinventing the wheel, you may want to look at CakeDC's tags and utils plugins (the latter contains SluggableBehavior which will help with generating friendly URL parts, such as the subdomains).

Generating pages from a database

I'm looking for some help understanding how to generate pages from a database to create a catalog of items, each with different URLs. All I can seem to find through google are products that will do this for me, or full e-commerce solutions. I don't want a shopping cart! Just an inventory.
Also, perhaps someone could recommend their favorite/the best simple login solution.
Thank you so much for your time and any help, suggestions, comments, solutions.
I just posted a thorough solution to another question that is very closely-related to this question. I'll re-post it here for your convenience:
I would suggest using some of the MVC (Model, View, Controller) frameworks out there like KohanaPHP. It is essentially this. You're working in a strictly Object-Oriented environment. A simple page in Kohana, build entirely from a class would look like this:
class Home_Controller extends Controller
{
public function index()
{
echo "Hello World";
}
}
You would then access that page by visiting youur url, the class name, and the method name:
http://www.mysite.com/home/ (index() can be called after home/, but it's implicit)
When you start wanting to bring in database-activity, you'll start working with another Class called a Model. This will contain methods to interact with your database, like the following:
class Users_Model extends Model
{
public function count_users()
{
return $this->db->count_records('users');
}
}
Note here that I didn't write my own query. Kohana comes with an intuitive Query Builder.
That method would be called from within your Controller, the first class that we mentioned at the beginning of this solution. That would look like this:
class Home_Controller extends Controller
{
public function index()
{
$usersModel = new Users_Model;
$userCount = $usersModel->count_users();
echo "We have " . $userCount . " users!";
}
}
Eventually, you'll want more complicated layouts, which will involve HTML/CSS/Javascript. At this point, you would introduce the "Views," which are just presentation layers. Rather than calling echo or print from within the Controller, you would load up a view (an HTML page, essentially) and pass it some variables:
class Home_Controller extends Controller
{
public function index()
{
$myView = new View("index");
$usersModel = new Users_Model;
$userCount = $usersModel->count_users();
$myView->userCount = $userCount;
$myView->render(TRUE);
}
}
Which would load the following "View"
<p>We have <?php print $userCount; ?> users!</p>
That should be enough to get you started. Using the MVC-style is really clean, and very fun to work with.
There's a lot of tools out there for generating a web interface around a data model. I find Django pretty easy to use. Based on its popularity, I'm sure that Ruby on Rails is another viable option.

Resources