如何编写BasePage类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何编写BasePage类相关的知识,希望对你有一定的参考价值。
BasePage是ASPX页面的父类,在ASP.NET开发中经常用到,主要包含一些页面常用的方法和Session的操作,使这些在各个子页面中通用。下面是我经常用的一个BasePage,希望你能帮助你理解,BasePage要继承System.Web.UI.Page,各个子页面则需要继承BasePage就可以使用里面的变量和方法了。public partial class 子类1: BasePage
///////////////BasePage源码///////////////
public class BasePage : System.Web.UI.Page
static BasePage()
public string BaseUrl
get
string sUrl = Request.Url.AbsoluteUri;
return sUrl.Substring(0, sUrl.LastIndexOf("/"));
/// <summary>
/// 登录人ID,对应Users里的USERID
/// </summary>
public string USERID
get
if (Session["USERID"] != null)
return Session["USERID"].ToString();
else
return "";
set
Session["USERID"] = value;
#region 页面加载流程
private void Page_Load(object sender, System.EventArgs e)
Page_LoadAlwayInit();
Page_LoadAlways();
if (IsPostBack)
Page_LoadPostBack();
else
Page_LoadOnce();
protected virtual void Page_LoadOnce()
protected virtual void Page_LoadPostBack()
protected virtual void Page_LoadAlways()
private void Page_LoadAlwayInit()
#endregion
#region 基类方法
public void AlertMessage(string title ,string msg)
msg = msg.Replace("\\", "\\\\");
msg = msg.Replace("\r\n", "\\n");
msg = msg.Replace("\n", "\\n");
msg = msg.Replace("\"", "\\\"");
string script = "";
if(title == null)
script = "<script>Ext.onReady(function()Ext.MessageBox.alert('" + msg + "');)</script>";
else
script = "<script>Ext.onReady(function()Ext.MessageBox.alert('" + title + "', '" + msg + "');)</script>";
ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
/// <summary>
/// 弹出页面
/// </summary>
/// <param name="url"></param>
public void OpenUrl(string url)
string script = string.Format("<script language='javascript'>window.open('" + "0" + "','" + "','');<", url);
script += "/";
script += "script>";
ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), script, false);
/// <summary>
/// 返回上一页
/// </summary>
public void JavascriptGoBack()
string script = "<script>history.go(-1);</script>";
ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
/// <summary>
/// 执行JavaScript方法
/// </summary>
/// <param name="script"></param>
public void JavascriptFun(string script)
string AllScript = string.Format("<script>0</script>", script);
ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), AllScript);
/// <summary>
/// 关闭窗口
/// </summary>
public void JavascriptClose()
string script = "<script>window.opener = null;window.close();</script>";
ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
/// <summary>
/// 跳转到其他页面
/// </summary>
/// <param name="url">页面地址</param>
public void JavascriptGoUrl(string url)
string script = string.Format("<script>location = '0';</script>", url);
ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
/// <summary>
/// 显示信息并跳转页面
/// </summary>
/// <param name="message"></param>
/// <param name="url"></param>
public void ShowMessageAndGoUrl(string message, string url)
string script = string.Format("<script>alert('0');location.href='1';</script>", message, url);
ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
/// <summary>
/// 获取MD5值
/// </summary>
/// <param name="data">原字符串</param>
/// <returns>通过MD5加密后的</returns>
public string GetMd5Code(string data)
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(data, "md5").ToUpper();
/// <summary>
/// 返回上一页
/// </summary>
protected bool GoBack()
if (ViewState["BackUrl"] != null)
Response.Redirect(ViewState["BackUrl"].ToString());
return true;
else
return false;
/// <summary>
/// 将列转化成字符串形式用逗号分隔
/// </summary>
/// <param name="columns">列字符串</param>
public string GetTableColumns(DataColumnCollection columns)
string title = "";
foreach (DataColumn col in columns)
title += GetSafeString(col.ColumnName).Replace(',', '_') + ",";
return title.Remove(title.Length - 1);
public static string CutString(string str, int maxlength)
if (str.Length > maxlength)
return str.Substring(0, maxlength) + "....";
else return str;
#endregion 基类方法
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
this.Load += new System.EventHandler(this.Page_Load);
#endregion
参考技术A 你的BasePage是指一个asp.net后台页面的基类吧,那你这个基类必须继承asp.net的基类System.Web.UI.Page,然可以重写一些事件和方法(比如OnPreInit,OnInit等)供子类调用。 参考技术B 将常量,如:URL参数,Session参数,页面跳转参数
公用方法,如过滤字特殊符串,验证等等..
selenium学习中级篇 -20封装BasePage基础类
在Base包中新建一个BasePage.py 这个文件中,主要封装一些基础的操作,比如说打开url,关闭浏览器,点击,输入,获取元素等与业务逻辑无关的最底层的操作
这个类,可以根据不同的项目需求进行拓展,本文只是举了一个简单的例子
class BasePage(object): def __init__(self, driver): self.driver = driver # 打开url def get_url(self, url): self.driver.get(url) # 退出浏览器 def quit_browser(self): self.driver.quit() # 获取元素 def find_element(self, selector): element = ‘‘ if ‘=>‘ not in selector: return selector.driver.find_element_by_xpath(selector) selector_by = selector.split(‘=>‘)[0] selector_value = selector.split(‘=>‘)[1] if selector_by == ‘id‘: element = self.driver.find_element_by_id(selector_value) elif selector_by == ‘name‘: element = self.driver.find_element_by_name(selector_value) elif selector_by == ‘xpath‘: element = self.driver.find_element_by_xpath(selector_value) elif selector_by == ‘linkText‘: element = self.driver.find_element_by_link_text(selector_value) elif selector_by == ‘partialLinkText‘: element = self.driver.find_element_by_partial_link_text(selector_value) elif selector_by == ‘css‘: element = self.driver.find_element_by_css_selector(selector_value) return element # 输入字符 def type(self, selector, text): el = self.find_element(selector) el.clear() el.send_keys(text) # 清空 def clear(self, selector): el = self.find_element(selector) el.clear() # 点击操作 def click(self, selector): el = self.find_element(selector) el.click() # 获取页面title def get_page_title(self): return self.driver.title # 获取当前页面的url def get_page_url(self): return self.driver.current_url
以上是关于如何编写BasePage类的主要内容,如果未能解决你的问题,请参考以下文章