调用方法时的NameError

时间:2015-08-12 07:54:59

标签: python appium

我有两个文件:

其中包含我的所有功能 funclib.py

  private async void accAcctDet_Click(object sender, RoutedEventArgs e)
    {
                string selected = acct[cmbAccDetail.SelectedIndex];
                string maskselect = cmbAccDetail.SelectedItem.ToString();
                prog.Visibility = Visibility.Visible;    
                acctdetails obj = new acctdetails();
                var result = await obj.GetToken(selected);                   
                if (result.Substring(0, 2) == "Se" || result.Substring(0, 2) == "Ki")
                {
                    prog.Visibility = Visibility.Collapsed;
                    MessageDialog msgbox = new MessageDialog("Service Unavailable. Please try again later", "e-Book!");
                    await msgbox.ShowAsync();
                    ContentRoot.Visibility = Visibility.Visible;
                    myCommandBar.Visibility = Visibility.Visible;
                }
                else
                {
                    prog.Visibility = Visibility.Collapsed;
                    this.Frame.Navigate(typeof(acctdetinfo), result);
                }
    }
    public async Task<string> GetToken(string selected)
    {
        string postData = "selAcct=" + selected;
        var res = await post("myparameter", postData);
        return res;
    }
    private async Task<string> post(string url, string postdata)
    {
        var request = WebRequest.Create(new Uri("myurl.com" + url)) as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "application/json";
        byte[] data = Encoding.UTF8.GetBytes(postdata);
        //request.ContentLength = data.Length;
        using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
        {
            await requestStream.WriteAsync(data, 0, data.Length);
        }
        WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
        var responseStream = responseObject.GetResponseStream(); 
        var sr = new StreamReader(responseStream);
        string received = await sr.ReadToEndAsync();
        return received;
    }

我还有另一个文件 scripts.py ,它会调用此函数。

import os 
from selenium import webdriver 
from selenium.webdriver.firefox.webdriver import WebDriver 
from selenium.webdriver.common.action_chains import ActionChains 
import time

        def deviceSelection():
            desired_caps = {}
            desired_caps['appium-version'] = '1.0'
            desired_caps['platformName'] = 'iOS'
            desired_caps['platformVersion'] = '8.2'
            desired_caps['deviceName'] = 'iPhone 6'
            desired_caps['app'] = os.path.abspath('/Users/admin/Library/Developer/Xcode/DerivedData/testapp-bfpdodvceugohuaaiiukkrcsrdqh/Build/Products/Debug-iphonesimulator/testapp.app')
            global wd
            wd = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
            wd.implicitly_wait(60)

当我执行scripts.py时,我收到错误

import os
from selenium import webdriver
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import sys
sys.path.append('/Users/admin/Desktop/Suite/Global Scripts/')
from funcLib import *

deviceSelection();

def firsttri():
    wd.find_element_by_name("Accept").click()
    time.sleep(5)   
    wd.find_element_by_name("Sign In").click()
    time.sleep(5)
    wd.find_element_by_name("Need help?").click()
    time.sleep(5)
    wd.find_element_by_name("Close").click()
    time.sleep(5)


firsttri();

不确定原因。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您对Python中的范围界定方式有点困惑。

当您致电deviceSelection()时,它没有在wd文件中创建global scripts.py,它在全球范围内funclib.py文件,以及您无法调用的原因。

有一些方法可以安排它,以便全球使用可行,但它们会变得混乱,你不应该这样做。相反,您应该删除globalreturn wd对象的使用。

def deviceSelection():
    desired_caps = {}
    desired_caps['appium-version'] = '1.0'
    desired_caps['platformName'] = 'iOS'
    desired_caps['platformVersion'] = '8.2'
    desired_caps['deviceName'] = 'iPhone 6'
    desired_caps['app'] = os.path.abspath('/Users/admin/Library/Developer/Xcode/DerivedData/testapp-bfpdodvceugohuaaiiukkrcsrdqh/Build/Products/Debug-iphonesimulator/testapp.app')
    wd = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
    wd.implicitly_wait(60)
    return wd

因为那样你就可以在wd内设置scripts.py

wd = deviceSelection()

一般情况下,建议您使用return语句而不是global将变量从一个名称空间传递到另一个名称空间,因为它更清晰,更明确,并且避免混淆这样的情况。