I have to write a test for the login dialog that shows up on my website, but there are two, and only two access points for this login dialog. Ideally, my page objects should reflect the restricted access to this login dialog.
-
When you
clickLogin
on theHeader
, aLoginDialog
pops up -
When you
postComment
on anArticle
, and you aren't logged in, aLoginDialog
pops up.
Here's what it looks like in code:
new LoginDialog().login(); // shouldn't be allowed
new Header().clickLogin().login(); // should be allowed
new Article().postComment().login() // should be allowed
However, I had some trouble coming up with a sensible method for allowing only Header
and Article
to initialize a LoginDialog
.
What I ended up using seems pretty esoteric, so I'll try my best to explain it. Essentially, LoginDialog
only has two constructors, which both take in an object that can only be constructed in either Header
or Article
.
public class Main {
public static void main(String [] args) {
new Header().clickLogin().login();
logout();
new Article().postComment().login();
}
}
public class LoginDialog {
public LoginDialog(Article._ article) {
}
public LoginDialog(Header._ header) {
}
public void login() {
System.out.println("Logged in!");
}
}
public class Article {
public class _ {
private _() {
}
}
public LoginDialog postComment() {
if (!loggedIn()) {
return new LoginDialog(new _());
} else {
return null;
}
}
}
public class Header {
public class _{
private _() {
}
}
public LoginDialog clickLogin() {
if (!loggedIn()) {
return new LoginDialog(new _());
} else {
return null;
}
}
}
To be honest, I'm not quite sure of what my question is. I guess I want to know whether or not this is a pattern that's been used elsewhere, and if it is, what is its name? Otherwise, is there a better way to accomplish what I want?
Aucun commentaire:
Enregistrer un commentaire