C# Function that can return either of two different page objects
I have a function that returns a WelcomeScreen
page object as follows:
public WelcomeScreen UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
return new WelcomeScreen(Driver);
}
Now, I want to access this same method but would like it to return another page object LessonPage
(return new LessonPage(Driver)
). Is there any way to accomplish this using the same method?
c# function selenium selenium-webdriver return-value
add a comment |
I have a function that returns a WelcomeScreen
page object as follows:
public WelcomeScreen UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
return new WelcomeScreen(Driver);
}
Now, I want to access this same method but would like it to return another page object LessonPage
(return new LessonPage(Driver)
). Is there any way to accomplish this using the same method?
c# function selenium selenium-webdriver return-value
add a comment |
I have a function that returns a WelcomeScreen
page object as follows:
public WelcomeScreen UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
return new WelcomeScreen(Driver);
}
Now, I want to access this same method but would like it to return another page object LessonPage
(return new LessonPage(Driver)
). Is there any way to accomplish this using the same method?
c# function selenium selenium-webdriver return-value
I have a function that returns a WelcomeScreen
page object as follows:
public WelcomeScreen UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
return new WelcomeScreen(Driver);
}
Now, I want to access this same method but would like it to return another page object LessonPage
(return new LessonPage(Driver)
). Is there any way to accomplish this using the same method?
c# function selenium selenium-webdriver return-value
c# function selenium selenium-webdriver return-value
edited Nov 14 '18 at 13:46
Guy
18.7k72249
18.7k72249
asked Nov 14 '18 at 12:51
melleckmelleck
598
598
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can create a private method with the logic and warp it with two methods, each return a page object
private void UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
}
public WelcomeScreen UpdateAndSubmitProfileAndGoToWelcomeScreen()
{
UpdateAndSubmitProfile();
return new WelcomeScreen(Driver);
}
public LessonPage UpdateAndSubmitProfileAndGoToLessonPage()
{
UpdateAndSubmitProfile();
return new LessonPage(Driver);
}
yes, I have used this technique in another place but is there any other better way to accomplish this?
– melleck
Nov 14 '18 at 13:10
1
@melleck IMHO this is the best way, and this is how I work. You can return parent class/interface, but if you need a method declared in the derived class you will have to cast the returned object.
– Guy
Nov 14 '18 at 13:13
add a comment |
Well if both Classes implement one interface you can change the return type of the Method to the Interface.
public Screen UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
if(...)
return new WelcomeScreen(Driver);
else
return new LessonPage(Driver);
}
Both classes extend from a BaseApplicationPage class.
– melleck
Nov 14 '18 at 13:07
then you can use the base application class as well. but pulling the base class out into a interface and have them implement the interface would be best imo
– Salim Proctor
Nov 14 '18 at 13:50
You could also pass in a parameter toUpdateAndSubmitProfile()
that controls thatif
flow to let the method know where you intend to end up and control the return that way.
– JeffC
Nov 14 '18 at 14:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300680%2fc-sharp-function-that-can-return-either-of-two-different-page-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can create a private method with the logic and warp it with two methods, each return a page object
private void UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
}
public WelcomeScreen UpdateAndSubmitProfileAndGoToWelcomeScreen()
{
UpdateAndSubmitProfile();
return new WelcomeScreen(Driver);
}
public LessonPage UpdateAndSubmitProfileAndGoToLessonPage()
{
UpdateAndSubmitProfile();
return new LessonPage(Driver);
}
yes, I have used this technique in another place but is there any other better way to accomplish this?
– melleck
Nov 14 '18 at 13:10
1
@melleck IMHO this is the best way, and this is how I work. You can return parent class/interface, but if you need a method declared in the derived class you will have to cast the returned object.
– Guy
Nov 14 '18 at 13:13
add a comment |
You can create a private method with the logic and warp it with two methods, each return a page object
private void UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
}
public WelcomeScreen UpdateAndSubmitProfileAndGoToWelcomeScreen()
{
UpdateAndSubmitProfile();
return new WelcomeScreen(Driver);
}
public LessonPage UpdateAndSubmitProfileAndGoToLessonPage()
{
UpdateAndSubmitProfile();
return new LessonPage(Driver);
}
yes, I have used this technique in another place but is there any other better way to accomplish this?
– melleck
Nov 14 '18 at 13:10
1
@melleck IMHO this is the best way, and this is how I work. You can return parent class/interface, but if you need a method declared in the derived class you will have to cast the returned object.
– Guy
Nov 14 '18 at 13:13
add a comment |
You can create a private method with the logic and warp it with two methods, each return a page object
private void UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
}
public WelcomeScreen UpdateAndSubmitProfileAndGoToWelcomeScreen()
{
UpdateAndSubmitProfile();
return new WelcomeScreen(Driver);
}
public LessonPage UpdateAndSubmitProfileAndGoToLessonPage()
{
UpdateAndSubmitProfile();
return new LessonPage(Driver);
}
You can create a private method with the logic and warp it with two methods, each return a page object
private void UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
}
public WelcomeScreen UpdateAndSubmitProfileAndGoToWelcomeScreen()
{
UpdateAndSubmitProfile();
return new WelcomeScreen(Driver);
}
public LessonPage UpdateAndSubmitProfileAndGoToLessonPage()
{
UpdateAndSubmitProfile();
return new LessonPage(Driver);
}
answered Nov 14 '18 at 13:08
GuyGuy
18.7k72249
18.7k72249
yes, I have used this technique in another place but is there any other better way to accomplish this?
– melleck
Nov 14 '18 at 13:10
1
@melleck IMHO this is the best way, and this is how I work. You can return parent class/interface, but if you need a method declared in the derived class you will have to cast the returned object.
– Guy
Nov 14 '18 at 13:13
add a comment |
yes, I have used this technique in another place but is there any other better way to accomplish this?
– melleck
Nov 14 '18 at 13:10
1
@melleck IMHO this is the best way, and this is how I work. You can return parent class/interface, but if you need a method declared in the derived class you will have to cast the returned object.
– Guy
Nov 14 '18 at 13:13
yes, I have used this technique in another place but is there any other better way to accomplish this?
– melleck
Nov 14 '18 at 13:10
yes, I have used this technique in another place but is there any other better way to accomplish this?
– melleck
Nov 14 '18 at 13:10
1
1
@melleck IMHO this is the best way, and this is how I work. You can return parent class/interface, but if you need a method declared in the derived class you will have to cast the returned object.
– Guy
Nov 14 '18 at 13:13
@melleck IMHO this is the best way, and this is how I work. You can return parent class/interface, but if you need a method declared in the derived class you will have to cast the returned object.
– Guy
Nov 14 '18 at 13:13
add a comment |
Well if both Classes implement one interface you can change the return type of the Method to the Interface.
public Screen UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
if(...)
return new WelcomeScreen(Driver);
else
return new LessonPage(Driver);
}
Both classes extend from a BaseApplicationPage class.
– melleck
Nov 14 '18 at 13:07
then you can use the base application class as well. but pulling the base class out into a interface and have them implement the interface would be best imo
– Salim Proctor
Nov 14 '18 at 13:50
You could also pass in a parameter toUpdateAndSubmitProfile()
that controls thatif
flow to let the method know where you intend to end up and control the return that way.
– JeffC
Nov 14 '18 at 14:37
add a comment |
Well if both Classes implement one interface you can change the return type of the Method to the Interface.
public Screen UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
if(...)
return new WelcomeScreen(Driver);
else
return new LessonPage(Driver);
}
Both classes extend from a BaseApplicationPage class.
– melleck
Nov 14 '18 at 13:07
then you can use the base application class as well. but pulling the base class out into a interface and have them implement the interface would be best imo
– Salim Proctor
Nov 14 '18 at 13:50
You could also pass in a parameter toUpdateAndSubmitProfile()
that controls thatif
flow to let the method know where you intend to end up and control the return that way.
– JeffC
Nov 14 '18 at 14:37
add a comment |
Well if both Classes implement one interface you can change the return type of the Method to the Interface.
public Screen UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
if(...)
return new WelcomeScreen(Driver);
else
return new LessonPage(Driver);
}
Well if both Classes implement one interface you can change the return type of the Method to the Interface.
public Screen UpdateAndSubmitProfile()
{
LastNameField.Clear();
LastNameField.SendKeys("Malik");
Reporter.LogPassingTestStepToBugLogger("Update Last Name profile field, Last Name => Malik");
ProfileSubmitButton.Click();
Reporter.LogPassingTestStepToBugLogger("Click Submit button.");
if(...)
return new WelcomeScreen(Driver);
else
return new LessonPage(Driver);
}
answered Nov 14 '18 at 12:56
FlorianOlchingFlorianOlching
1044
1044
Both classes extend from a BaseApplicationPage class.
– melleck
Nov 14 '18 at 13:07
then you can use the base application class as well. but pulling the base class out into a interface and have them implement the interface would be best imo
– Salim Proctor
Nov 14 '18 at 13:50
You could also pass in a parameter toUpdateAndSubmitProfile()
that controls thatif
flow to let the method know where you intend to end up and control the return that way.
– JeffC
Nov 14 '18 at 14:37
add a comment |
Both classes extend from a BaseApplicationPage class.
– melleck
Nov 14 '18 at 13:07
then you can use the base application class as well. but pulling the base class out into a interface and have them implement the interface would be best imo
– Salim Proctor
Nov 14 '18 at 13:50
You could also pass in a parameter toUpdateAndSubmitProfile()
that controls thatif
flow to let the method know where you intend to end up and control the return that way.
– JeffC
Nov 14 '18 at 14:37
Both classes extend from a BaseApplicationPage class.
– melleck
Nov 14 '18 at 13:07
Both classes extend from a BaseApplicationPage class.
– melleck
Nov 14 '18 at 13:07
then you can use the base application class as well. but pulling the base class out into a interface and have them implement the interface would be best imo
– Salim Proctor
Nov 14 '18 at 13:50
then you can use the base application class as well. but pulling the base class out into a interface and have them implement the interface would be best imo
– Salim Proctor
Nov 14 '18 at 13:50
You could also pass in a parameter to
UpdateAndSubmitProfile()
that controls that if
flow to let the method know where you intend to end up and control the return that way.– JeffC
Nov 14 '18 at 14:37
You could also pass in a parameter to
UpdateAndSubmitProfile()
that controls that if
flow to let the method know where you intend to end up and control the return that way.– JeffC
Nov 14 '18 at 14:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300680%2fc-sharp-function-that-can-return-either-of-two-different-page-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown