app端自动化POM模型和非pom模型实现QQ登录

 非pom模型

 pom模型

1、在公共的模块中写入

caps:
    platformName : Android
    deviceName : 127.0.0.1:62001
    appActivity : com.tencent.mobileqq.activity.LoginActivity
    appPackage : com.tencent.mobileqq

2、编写读取yaml

import yaml,os
def yeml_method(path):
    with open(path, "r", encoding="utf-8") as file:
        data = yaml.load(stream=file, Loader=yaml.FullLoader)
        return data
# print(os.path.dirname(__file__))
# print(os.path.dirname(os.path.dirname(__file__)))
# print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
# print(os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))),"testdata/yeml_read"))
print(yeml_method(os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))),"basepage/casepage.yaml")))

3、导航界面

from appium.webdriver.common.mobileby import MobileBy
from basepage.basepage import BasePages
class DaHangpage(BasePages):
    def __init__(self,driver):
        BasePages.__init__(self,driver)
    def click_login(self):
        self.click(MobileBy.ID,"com.tencent.mobileqq:id/btn_login")

4、登录界面

from basepage.basepage import BasePages
from appium.webdriver.common.mobileby import MobileBy
class LoginPage(BasePages):
    def send_zh(self,test):
        self.input(test,MobileBy.ACCESSIBILITY_ID,"请输入QQ号码或手机或邮箱")
    def send_mm(self,test):
        self.input(test,MobileBy.ACCESSIBILITY_ID,"密码 安全")
    def click_login_button(self):
        self.click(MobileBy.ACCESSIBILITY_ID,"登 录")

5、公共方法

class BasePages:
    def __init__(self,driver):
        self.driver = driver
        #元素定位
    def locator(self, *loc):
            return self.driver.find_element(*loc)
        # 清空
    def clear(self,*loc):
            self.locator(*loc).clear()
        # 输入
    def input(self,test,*loc):
            self.locator(*loc).send_keys(test)
        # 点击
    def click(self,*loc):
            self.locator(*loc).click()
        # 滑动(上下左右滑动)
    def swipe(self,start_x,start_y,end_x,end_y,duration=0):
        #获取屏幕的尺寸
        window_size = self.driver.get_window_size()
        x = window_size["width"]
        y = window_size["height"]
        self.driver.swipe(start_x=x*start_x,
                            start_y=y*start_y,
                            end_x= x*end_x,
                            end_y=y*end_y,
                            duration=duration)

6、编写测试用例

import unittest,os
from appium import webdriver
from baseobject.basedj import DaHangpage
from baseobject.basedl import LoginPage
from testdata.readyaml import yeml_method
import pytest
class TestClass(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        data = yeml_method(os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))),"basepage/casepage.yaml"))
        # caps = {}
        # caps["platformName"] = "Android"
        # caps["deviceName"] = "127.0.0.1:62001"
        # caps["appPackage"] = "com.tencent.mobileqq"
        # caps["appActivity"] = "com.tencent.mobileqq.activity.LoginActivity"
        # caps["platformVersion"] = "5.1.1"

        cls.driver = webdriver.Remote("http://localhost:4723/wd/hub", data["caps"])
        cls.driver.implicitly_wait(30)
    def test_01(self):
        dh = DaHangpage(self.driver)
        dh.click_login()
    @pytest.mark.parametrize("username,password",[("QQ号","QQ密码")])
    def test_02(self,username,password):
        lg = LoginPage(self.driver)
        lg.send_zh("username")
        lg.send_mm("password")
        lg.click_login_button()
    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.quit()
if __name__ == '__main__':
    unittest.main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现第三方qq登录,可以使用QQ互联提供的开放平台接口。具体的步骤如下: 1. 注册成为QQ互联开发者,并创建应用。在创建应用时,需要填写应用的基本信息,并获得AppID和AppKey。 2. 在Spring Boot后项目中,使用Spring Security实现OAuth2认证。在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-oauth2-client</artifactId> <version>5.3.4.RELEASE</version> </dependency> ``` 3. 在前Vue项目中,使用Vue CLI创建项目,并安装vue-cli-plugin-qrcode实现生成二维码。在Terminal中运行以下命令: ``` vue create qq-login-demo cd qq-login-demo vue add qrcode ``` 4. 在Spring Boot后项目中,实现OAuth2的配置。在application.yml文件中添加以下配置: ```yaml spring: security: oauth2: client: registration: qq: clientId: <your app id> clientSecret: <your app key> redirectUriTemplate: "{baseUrl}/login/oauth2/code/{registrationId}" authorizationUri: https://graph.qq.com/oauth2.0/authorize tokenUri: https://graph.qq.com/oauth2.0/token userInfoUri: https://graph.qq.com/user/get_user_info scope: - get_user_info provider: qq: authorizationUri: https://graph.qq.com/oauth2.0/authorize tokenUri: https://graph.qq.com/oauth2.0/token userInfoUri: https://graph.qq.com/user/get_user_info?oauth_consumer_key={clientId}&openid={oauthId} userNameAttribute: nickname ``` 5. 在前Vue项目中,实现二维码生成。在App.vue文件中添加以下代码: ```vue <template> <div id="app"> <div v-if="!isLogin"> <qrcode :value="qrCodeUrl"></qrcode> </div> <div v-else> <p>{{ userInfo.nickname }}</p> <img :src="userInfo.figureurl_qq_1" alt="头像"> </div> </div> </template> <script> import QRCode from 'qrcode' export default { name: 'App', data() { return { isLogin: false, qrCodeUrl: '' } }, mounted() { this.generateQRCode() }, methods: { async generateQRCode() { const response = await this.$http.get('/qq-login/qr-code') if (response.status === 200) { this.qrCodeUrl = response.data this.isLogin = false } }, async checkLoginStatus() { const response = await this.$http.get('/qq-login/check-login-status') if (response.status === 200) { this.userInfo = response.data this.isLogin = true } } } } </script> ``` 6. 在Spring Boot后项目中,实现二维码生成和登录状态检查。在QqLoginController.java文件中添加以下代码: ```java @RestController @RequestMapping("/qq-login") public class QqLoginController { private final OAuth2AuthorizedClientService authorizedClientService; private final RestTemplate restTemplate; @Autowired public QqLoginController(OAuth2AuthorizedClientService authorizedClientService, RestTemplate restTemplate) { this.authorizedClientService = authorizedClientService; this.restTemplate = restTemplate; } @GetMapping("/qr-code") public String generateQRCode() { String qrCodeUrl = "https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&client_id=<your app id>&redirect_uri=http://localhost:8080/login&state=<random state>"; try { qrCodeUrl = QRCode.from(qrCodeUrl).to(ImageType.PNG).withSize(250, 250).stream().collect(Collectors.toBase64String); } catch (WriterException | IOException e) { e.printStackTrace(); } return "data:image/png;base64," + qrCodeUrl; } @GetMapping("/check-login-status") public Map<String, Object> checkLoginStatus() { OAuth2AuthenticationToken authenticationToken = (OAuth2AuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); OAuth2AuthorizedClient authorizedClient = authorizedClientService.loadAuthorizedClient(authenticationToken.getAuthorizedClientRegistrationId(), authenticationToken.getName()); String accessToken = authorizedClient.getAccessToken().getTokenValue(); String openId = restTemplate.getForObject("https://graph.qq.com/oauth2.0/me?access_token=" + accessToken, String.class); openId = StringUtils.substringBetween(openId, "\"openid\":\"", "\"}"); Map<String, Object> userInfo = restTemplate.getForObject("https://graph.qq.com/user/get_user_info?access_token=" + accessToken + "&oauth_consumer_key=<your app id>&openid=" + openId, Map.class); return userInfo; } } ``` 7. 在前Vue项目中,实现登录状态检查。在App.vue文件中添加以下代码: ```vue <script> export default { name: 'App', data() { return { isLogin: false, qrCodeUrl: '' } }, mounted() { this.generateQRCode() this.checkLoginStatus() setInterval(() => { this.checkLoginStatus() }, 3000) }, methods: { async checkLoginStatus() { const response = await this.$http.get('/qq-login/check-login-status') if (response.status === 200) { this.userInfo = response.data this.isLogin = true } } } } </script> ``` 至此,我们就成功地实现了Spring Boot+Vue代码实现第三方QQ登录的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值